Modular page can't get custom variables from base template

Sometimes I get this annoying problem and I have no idea what causes it.

Form the base template file (partials/base.html.twig), I set the global variable for site and theme’s configuration.

{% set theme_config = attribute(config.themes, config.system.pages.theme) %}
<!DOCTYPE html>
<html lang="{{ grav.language.getLanguage ?: 'en' }}">
    <head>
        {% include "partials/head.html.twig" %}
    </head>
    <body>
        {% block content %},{% endblock %}
    </body>
</html>

Here is my modular.html.twig:

{% extends 'partials/base.html.twig' %}

{% block content %}
    {% for module in page.collection() %}
        {# theme_config is still available here #}
        {{ module.content }}
    {% endfor %}
{% endblock %}

In my modular page theme__config variable is null. However I still can get config variable of Grav. This problem sometimes happens, sometimes not. My only solution right now is re-set theme__config variable in each modular page:

{% set theme_config = attribute(config.themes, config.system.pages.theme) %}

If you have any idea of why this happens, please let me know. Thanks!

Hmm, it seems in modular page the global variable “pages” is not accessible too. I need to use page.parent.parent.children instead of pages.children.