# Some background callbacks do not finish in a multi-page app

**URL:** <https://community.plotly.com/t/some-background-callbacks-do-not-finish-in-a-multi-page-app/91967>\
**Category:** Dash Python\
**Created:** [April 23, 2025, 2:15pm UTC](https://community.plotly.com/t/some-background-callbacks-do-not-finish-in-a-multi-page-app/91967 "2025-04-23T14:15:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![valsorim](https://avatars.discourse-cdn.com/v4/letter/v/e47774/32.png) [@valsorim](https://community.plotly.com/u/valsorim)\
**Post date:** [April 23, 2025, 2:15pm UTC](https://community.plotly.com/t/some-background-callbacks-do-not-finish-in-a-multi-page-app/91967/1 "2025-04-23T14:15:45Z")

</div>

Hi all, I encountered this weird behavior in the situation in the minimal example below.

How to reproduce: click on the button, wait for the background callback to finish, click again, repeat.

In this case the background task finishes every second time it is triggered. The other times there is a 204 response for `"POST /_dash-update-component?cacheKey=..."`

**Dash version**  
`dash[diskcache]==3.0.3`

**app.py**

```python
import diskcache
import dash
from dash import html, dcc

cache = diskcache.Cache("./cache")
background_callback_manager = dash.DiskcacheManager(diskcache.Cache("./cache_dm"))

app = dash.Dash(
    __name__ ,
    use_pages=True,
    suppress_callback_exceptions=True,
    background_callback_manager=background_callback_manager,
)

server = app.server

app.layout = html.Div([
    html.H1("Main App Layout"),
    dcc.Location(id="url"),
    dash.page_container
])

if __name__ == " __main__":
    app.run(debug=False)

```

**pages/home.py**

```python
import time
import dash
from dash import html, dcc, Input, Output, callback, clientside_callback

dash.register_page( __name__ , path="/", name="Home")

layout = html.Div([
    html.H2("Home Page with Button and Background Task"),
    html.Button("Click Me", id="main-button", n_clicks=0),
    dcc.Store(id="dummy-store"),
    dcc.Store(id="reload-trigger"),
])

clientside_callback(
    """
    function(data) {
        if (data) {
            window.location.reload();
        }
        return null;
    }
    """,
    Output("dummy-store", "data"),
    Input("reload-trigger", "data"),
    prevent_initial_call=True
)

@callback(
    Output("reload-trigger", "data"),
    Input("main-button", "n_clicks"),
    prevent_initial_call=True
)
def update_store(n_clicks):
    time.sleep(5)
    return True

@callback(
    Input("main-button", "n_clicks"),
    background=True,
    prevent_initial_call=True
)
def long_running_task(n_clicks):
    print("Background task started...")
    time.sleep(10)
    print("Background task finished.")

```

Any ideas on how to correct it? I would need for the app to finish all background tasks even if the window is reloaded.

Thank you.
