Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Issue with cache key edge case #3189

Open
MartinSA04 opened this issue Feb 25, 2025 · 0 comments · May be fixed by #3190
Open

[BUG] Issue with cache key edge case #3189

MartinSA04 opened this issue Feb 25, 2025 · 0 comments · May be fixed by #3190

Comments

@MartinSA04
Copy link

MartinSA04 commented Feb 25, 2025

dash                 2.18.2
dash-core-components 2.0.0
dash-html-components 2.0.0
dash-table           5.0.0

Describe the bug
When a background callback resets one of its own outputs, you cannot set the value of that output back to the previous value, because the basis for the cache key would be the same, and the cached result would be returned. This is an edge case, but it happens frequently enough to warrant a change.

Expected behavior

A callback invoked with the same input values, but different triggered inputs should not retrieve the cached result.

minimal example:
Select "World", and wait for the callback to finish. Press "clear page", and wait for the callback to finish. If you now try to select "World", you cannot do so.

import dash
from dash import html, dcc, Input, Output, no_update
import diskcache
from dash import DiskcacheManager
from uuid import uuid4
import time

launch_uid = uuid4()

cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(
    cache, cache_by=[lambda: launch_uid], expire=60
)

app = dash.Dash(__name__, background_callback_manager=background_callback_manager)

app.layout = html.Div(
    [
        dcc.RadioItems(["Hello", "World"], id="input", value="Hello"),
        html.Div(id="output", children="Hello is selected"),
        html.Button("clear page", id="button"),
    ]
)

@app.callback(
    Output("output", "children"),
    Output("input", "value"),
    Input("input", "value"),
    Input("button", "n_clicks"),
    prevent_initial_call=True,
    background=True
)
def update_output(value, n_clicks):
    triggered_id = dash.callback_context.triggered_id
    print(triggered_id)
    time.sleep(5)
    match triggered_id:
        case "input":
            return value + " is selected", no_update
        case "button":
            return "Hello is selected", "Hello"
        case _:
            return no_update, no_update

if __name__ == "__main__":
    app.run_server()
@MartinSA04 MartinSA04 changed the title [BUG] [BUG] Issue with cache key edge case Feb 25, 2025
@MartinSA04 MartinSA04 linked a pull request Feb 25, 2025 that will close this issue
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant