Skip to content

Background channel tasks: off-loop sessions, burst-safe webhooks - #532

Open
chattermate wants to merge 6 commits into
mainfrom
feature/background-tasks-off-loop
Open

chattermate wants to merge 6 commits into
mainfrom
feature/background-tasks-off-loop

Conversation

@chattermate

@chattermate chattermate commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Channel webhook background tasks (inbound messages, delivery receipts, interactions, the Slack Home and assistant events, ticket email replies) opened SessionLocal() on the event loop. In a burst, the loop-safe pool refused them, so customers didn't get replies. This PR also stops a burst stalling the dashboard.

  • The tasks open their session with session_off_loop(), and call release_connection() before any outside call (extensions, the app handler, the profile fetch, Slack).
  • Webhook routes use webhook_db, a function-scoped get_db, so the request's session is closed before its background tasks run. Before this, a request held its slot while its own task waited for one; ten webhooks at once froze the backend.
  • Channel tasks queue for one of MAX_CONCURRENT_CHANNEL_TASKS places (default 4, at most half for any one account), so a burst queues instead of running the pool dry.
  • New e2e/scripts/channel-burst.mjs, added to the nightly burst variant: 100 WhatsApp messages and 200 receipts at once must all be answered, with no API stall and no pool-full warning. On main the API stalls 6–7s and the pool overflows; on this branch it's under 0.5s, with no warnings.
  • tests/socket_db.py is now tests/session_db.py, with a generic serve_session_off_loop helper.

Tested: backend suite (OSS); channels, integrations, team and tickets e2e specs; both burst scripts.

Channel message, interaction and status handlers and the Slack Home/
assistant events opened SessionLocal() on the event loop. The loop-safe
pool keeps that from freezing the server, but in a burst it refuses them
at once, dropping the reply to an inbound message. session_off_loop()
makes them wait their turn in a checkout thread instead.

Signed-off-by: Runix <admin@chattermate.chat>
Background tasks now share session_off_loop's slots with requests, so
holding one through an extension, app, platform or Slack call slowed the
dashboard during webhook bursts. Each task reads what it needs, then
release_connection()s before waiting on anything outside.

Also ports the ticket email-reply task, which still opened SessionLocal()
on the event loop, and adds tests that each wait happens with the slot
free and that a receipt queues for a slot rather than failing. The shared
test helper moves to tests/session_db.py.

Signed-off-by: Runix <admin@chattermate.chat>
A new customer's profile fetch released the session before the session
and config reads, sending those back to the event loop. It now runs after
setup. app_claims and the ticket email-reply task also give the slot back
before their outside calls.

Signed-off-by: Runix <admin@chattermate.chat>
A webhook route held its request session (and off-loop slot) until its
background task finished, while that task waited for a slot of its own:
ten webhooks at once took every slot and froze the backend. Webhook routes
now close their session when the endpoint returns (webhook_db).

Channel tasks also ran unbounded, so a burst used up the pool from the
event loop and refused most of them. They now queue for one of
MAX_CONCURRENT_CHANNEL_TASKS places (default 4).

Adds e2e/scripts/channel-burst.mjs to the nightly burst variant: 100
WhatsApp messages and 200 receipts at once must all be answered without
the API stalling. On main the API stalls 6-7s under it.

Signed-off-by: Runix <admin@chattermate.chat>
One account whose AI provider hangs could take every place and keep every
other business's messages waiting. Also names LOOP_OVERFLOW so a test can
hold the task limit to the pool budget, and the channel burst now counts
button replies and fails on any pool-full warning.

Signed-off-by: Runix <admin@chattermate.chat>
@chattermate
chattermate force-pushed the feature/background-tasks-off-loop branch from 7c2a8df to 8adab52 Compare November 30, 2026 17:32
@chattermate chattermate changed the title Open background-task DB sessions off the event loop Background channel tasks: off-loop sessions, burst-safe webhooks Nov 30, 2026
Signed-off-by: Runix <admin@chattermate.chat>
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 this pull request may close these issues.

1 participant