Skip to content

fix: preserve context variables in threaded listeners - #1632

Open
noooooooookro wants to merge 1 commit into
slackapi:mainfrom
noooooooookro:fix/listener-context-propagation
Open

noooooooookro wants to merge 1 commit into
slackapi:mainfrom
noooooooookro:fix/listener-context-propagation

Conversation

@noooooooookro

Copy link
Copy Markdown

Summary

A threaded listener can lose a correlation ID set by middleware. Bolt now copies the current context each time it submits an ack or lazy callback, including Django lazy listeners. If one callback rebinds that ID, the next callback on the same worker still sees the value set by middleware.

Fixes #1267.

Testing

The full suite passed on Linux with Python 3.11 (957 tests). One regression test dispatches a shortcut through a single-worker app and checks that each listener sees the middleware value. A second sends a signed request through the Django adapter and checks its lazy listener.

./scripts/run_tests.sh tests/scenario_tests/test_listener_context.py
./scripts/run_tests.sh tests/adapter_tests/django/test_django.py::test_django_lazy_listener_context

Category

  • slack_bolt.App and/or its core components
  • slack_bolt.async_app.AsyncApp and/or its core components
  • Adapters in slack_bolt.adapter
  • Document pages under /docs
  • Others

Requirements

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I have signed the Salesforce CLA (required before your PR can be reviewed or merged).
  • I've run ./scripts/install_all_and_run_tests.sh after making the changes.

@noooooooookro
noooooooookro requested a review from a team as a code owner November 29, 2026 08:46
@salesforce-cla

Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @noooooooookro to sign the Salesforce Inc. Contributor License Agreement.

@mwbrooks mwbrooks added this to the next milestone Nov 30, 2026
@mwbrooks mwbrooks changed the title Preserve context variables in threaded listeners fix: preserve context variables in threaded listeners Nov 30, 2026
@mwbrooks

Copy link
Copy Markdown
Member

@noooooooookro Could you sign the Salesforce CLA? We need it signed before we can continue with this PR. 🙏

@mwbrooks mwbrooks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Thanks @noooooooookro for picking up #1267, and congrats on your first contribution to Bolt for Python! 🎉 Wrapping each submit in copy_context().run is the approach discussed on the issue, and it brings the sync runners in line with how asyncio already carries context into tasks. CI is next, we'll get the workflow runs started.

seen["ack"] = (get_ident(), request_id.get())
request_id.set("from-ack")
ack()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Rebinding the value in the ack listener and then checking it from the lazy listener and a third task on the same worker is a great way to prove each callback gets its own copy, not just the middleware value.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Looking for advice on ThreadPool separation for middleware and actual event, looking for ContextVars to be passed

2 participants