Skip to content

[BUG] state change for long_callback can cause infinite loop #2132

@kwikwag

Description

@kwikwag

Thank you so much for developing Dash!

  • pip list | grep dash
dash                          2.5.1
dash-auth                     1.4.1
dash-bootstrap-components     1.2.0
  • not frontend related

Describe the bug

Consider the following setup:

app.layout = h.Div([
    h.Button('Click me', id='button'),
    h.Div('', id='data'),
])

@app.callback(
    output=Output('data', 'children'),
    inputs=[Input('button', 'n_clicks')],
    state=[State('data', 'children')]
)
def clicked(n_clicks, data):
    if n_clicks is None:
        return str(0)
    return str(int(data) + 1)

Every time the user clicks the button, the callback will be called once and count will increase. Note that state and output point to the same variable.

However, if you replace @app.callback() with @app.long_callback() you will get an infinite loop. The reason for this is that Dash.long_callback() uses the input arguments -- including the 'state' arguments -- to create a unique key to identify the pending job. When the state changes, this key changes, and the job gets resubmitted.

The above is similar but not quite my use-case: I have a long-running multi-part job. I want the first part to be calculated only once, i.e. when the user first clicks the button, but not re-calculated every time the button is hit. For this reason I share 'state' and 'output' - the result of the first calculation gets cached in a store. In my case, the result of the first calculation doesn't change upon re-invoking the job, so the callback gets invoked exactly twice.

Expected behavior

@app.long_callback() should behave the same as @app.callback(), specifically, invocations due to 'state' changes should be avoided as they are not 'inputs' changes.

Screenshots

callback
long_callback

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions