Files
baserow/docs/development/background-chat-runs.md
Davide Silvestri f5bc919473 feat(celery): make every task work on a threads worker, and use one in dev (#6926)
* chore(dev): run one threads Celery worker with beat in dev

The Docker and native dev stacks start a single Celery worker for every
queue, with redbeat embedded, instead of a worker, an export worker and a
beat process. It uses a new ThreadsPool that sends worker_process_init, so
telemetry and the lazy import check still run under the threads pool.

* fix(ws): stop closing every event loop's Redis pools after a broadcast

send_messages_to_channel_group called channel_layer.close_pools(), which
closes the pools of every event loop, including the ones other worker
threads are still sending with. Under the threads pool about a third of
broadcasts failed with "attached to a different loop" and some hung.
channels_redis now closes a loop's pools itself when that loop closes.

* fix(telemetry): keep Celery trace context per thread on threads workers

The Celery instrumentation keys a running task's span, context token and start
time by the task's id. A task replaced with self.replace(), like every
automation node, keeps its id, and on a threads worker the replacement can
start in another thread before the original ends. The original then ended the
replacement's span and detached its context from the wrong thread, logging
"Failed to detach context" and leaving its own context on the thread. That
state is now kept per thread.

* feat(celery): enforce soft and hard time limits in ThreadsPool

Celery's own threads pool ignores soft_time_limit and time_limit, so on a
threads worker tasks that rely on them would run on. ThreadsPool now raises
the limit in the task's thread, only while that task runs, so a limit never
reaches the next task of the thread. EMAIL_TIMEOUT stops a silent SMTP server
from blocking a worker thread forever.

* fix(chat-runs): cancel a run that keeps going after its deadline

The deadline only asks a run to stop. A run whose tools or provider call
ignore that request now gets 60 more seconds, is then cancelled, and is saved
as timed out. Celery's limit moves after that, so the run's own stop always
comes first. On a threads worker this is the only way to end it, because a
time limit can't reach a thread waiting inside async_to_sync.

* fix(premium): share one capped thread pool across AI field generations

Each generation started its own pool of
BASEROW_AI_FIELD_MAX_CONCURRENT_GENERATIONS threads, so a threads worker
running several jobs multiplied the threads and model calls. All generations
in a process now share one pool of that size. A prefork process runs one job
at a time, so nothing changes there.

* feat(celery): cancel queries at the hard limit and report overdue tasks

ThreadsPool checks every running task's time limits from one watchdog thread,
like the prefork pool's timeout handler, instead of starting timer threads
for each task. At the hard limit it also cancels the query the task's thread
is running, which the limit's exception can't interrupt.

A thread can't be killed, so a task still running 60 seconds after its hard
limit is reported to Sentry instead. Every limit reached is counted in
baserow.celery.task.time_limit, per task and limit.

* fix(airtable): time out requests to Airtable

Two Airtable import requests had no timeout, so a server that stopped
answering held the import, and on a threads worker its thread, forever. All
Airtable requests now use the same 3 minute read timeout.

* fix(celery): load sentry_sdk only when reporting an overdue task

The overdue report imported sentry_sdk at the top of baserow.config.celery,
which every process imports, so it was loaded even with Sentry off.

* chore: add a changelog entry for the Celery task timeouts

* fix(celery): raise threads pool time limits at the task's next query

An exception raised into a thread from outside can land right after a lock
is taken and before the code that releases it, leaving the lock held for
every thread of the worker. The pool now marks a reached limit and the
task's own next query raises it, once, through a connection execute
wrapper. The hard limit still cancels the running query, and raises
TimeLimitExceeded whatever the soft limit is. Limits end before Celery's
success callback runs. Savepoint queries are never interrupted, through
a helper shared with the periodic field update deadline.

* fix(celery): apply the worker default time limits on the threads pool

Celery passes CELERY_TASK_SOFT_TIME_LIMIT and CELERY_TASK_TIME_LIMIT to the pool and None per task when a task sets no limit. The prefork pool falls back to the pool's values; the threads pool now does too.

* fix(celery): keep threads pool time limits until running tasks end

The watchdog stopped before on_stop waited for the running tasks, so a task passing its limit during a warm shutdown was never stopped or reported.

* chore: name the emails the SMTP timeout covers in the changelog

EMAIL_TIMEOUT applies to the instance's own emails only. Automation and builder SMTP steps already time out after 30 seconds.

* chore(dev): drop dev.sh tabs for the removed Celery workers

The export and beat services are disabled, so attaching to them made the script exit before opening the remaining tabs.

* test(celery): cover thread time limits and savepoint rollback

* fix(ai): use one generation slot for nonpositive limits

* fix(chat): warn that an action may finish after a reply times out

* docs(celery): explain which timeout guarantees require prefork

* docs(celery): record the threads pool decision and task contract

* fix(chat): keep the worker slot until started tools finish

* fix(chat): warn visitors about repeating a timed-out request

* docs(chat): clarify when the stop grace period begins
2026-12-03 09:39:52 +01:00

14 KiB

Background chat runs

An AI answer can take minutes. A chat run computes it in a Celery worker, not inside the HTTP request. The browser only follows the output. Refreshing the page, closing the tab or losing the connection does not stop the answer.

Use it for a chat-like feature where a user sends a message and watches a long, streamed answer, such as Kuma. For a quick request, a normal API view is simpler.

The engine lives in baserow.core.chat_runs (backend) and the core chatRuns module (frontend). It does not need enterprise. See ADR 010 for why it works this way.

The pieces

  • Run: a ChatRun row in PostgreSQL. It holds the status and, at the end, the saved result. It is the source of truth.
  • Worker: a Celery task. It computes the answer once, by calling the product's execute.
  • Journal: what execute writes output to. It batches output and sends it to the buffer.
  • Buffer: a Redis stream with a temporary copy of the output. A browser that joins late or reconnects replays it from the start. It can be lost.
  • Follower: one streaming HTTP response that reads the buffer for one browser tab. A run can have many followers. Closing one never stops the run.
  • Adapter: the product's ChatRunType. It decides who can see a run, computes the answer and saves it into the product's own conversation.
sequenceDiagram
    participant Browser
    participant API as Web process<br/>HTTP request
    participant DB as PostgreSQL
    participant Worker as Celery worker<br/>execute_chat_run()
    participant Buffer as Redis output<br/>buffer

    Browser->>API: Send message
    API->>DB: start_run(): create queued run<br/>in the message transaction
    DB-->>API: Committed
    API->>Worker: _enqueue() calls<br/>execute_chat_run.delay()
    Worker->>Worker: claim_run(), then run_chat_run()<br/>calls run_type.execute()

    Note over API: follow_run() runs in<br/>the HTTP response
    Note over Worker: execute() calls<br/>journal.emit_text() / emit()
    loop While the answer is being generated
        Worker->>Buffer: journal.flush()<br/>calls buffer.append()
        API->>Buffer: follow_run() calls buffer.read(cursor)
        Buffer-->>API: Output + cursor
        API-->>Browser: Stream output
    end

    Worker->>DB: _finish(): persist_outcome()<br/>+ finish_run() in one transaction
    DB-->>Worker: Committed
    Worker->>Buffer: publish_finished()
    API->>Buffer: buffer.read(cursor)
    Buffer-->>API: run.finished event
    API->>DB: _read_state()<br/>reads the saved run
    DB-->>API: Final result + status
    API-->>Browser: Final result,<br/>then close stream

The follower is the API's streaming HTTP response, not another worker. Redis holds the live output; PostgreSQL holds the saved result. If the browser disconnects, the Celery worker keeps running.

Websocket messages only say that a run started or ended. They never carry output.

What runs where

The HTTP request and Celery task run in separate processes. Calling execute_chat_run.delay(...) queues work for Celery; it does not execute the answer in the web process. After that, the worker and HTTP follower run independently.

Context Caller and method
HTTP: prepare the response AsyncStreamingHTTPRouter.__call__ runs _prepare in the web process's thread pool. _prepare calls Django's middleware and the product view.
HTTP: start a run The product view calls ChatRunHandler.start_run, which calls create_run. The view saves the user's message in the same transaction. After commit, _enqueue calls execute_chat_run.delay.
Celery: generate the answer execute_chat_run calls ChatRunHandler.claim_run. If it claims the run, it calls run_chat_run through async_to_sync. The runner awaits the product's ChatRunType.execute(run, journal).
Celery: publish output The adapter calls journal.emit_text or emit. The runner also starts journal.keep_flushing, which calls flush and then ChatRunBuffer.append to write batches to Redis.
Celery: watch for Stop The runner starts journal.keep_alive, which calls ChatRunHandler.renew_lease through sync_to_async. It sets journal.stop_requested when cancellation is requested. The adapter observes it and lets running tools finish.
Celery: save the answer When execute returns a ChatRunOutcome, the runner calls _finish through sync_to_async. _finish calls the adapter's persist_outcome and the handler's finish_run in one transaction. After commit, the runner calls journal.publish_finished.
HTTP: stream output The view returns an iterator without running it. On the HTTP event loop, _send_response consumes the product's iterator, which consumes follow_run. The follower calls ChatRunBuffer.read to wait for Redis output.
HTTP: check durable state follow_run calls _read_state through sync_to_async. When the stream opens, the core view checks can_read through ChatRunService.get_run and passes the serialized run to follow_run as initial_state. Later reads skip that check and happen only on run.finished or after 5 s without output. Each read calls serialize_outcome, which also supplies the final saved result.

async_to_sync and sync_to_async bridge synchronous and async code within the current process. They do not enqueue Celery tasks. The journal's flushing and lease checks are asyncio tasks inside the existing Celery task.

Refreshing opens a new HTTP follower with the last received cursor. It does not call start_run or enqueue another Celery task. Each journal.emit_text call supplies the whole answer so far; the journal turns it into deltas for Redis.

Stop is a separate HTTP request: POST /api/chat-runs/<id>/cancel/ calls ChatRunService.request_cancel, which checks can_cancel before calling ChatRunHandler.request_cancel. A queued run ends immediately. A running run gets a cancellation flag, which the worker observes through keep_alive.

At 15 minutes the runner asks the adapter to stop starting work. A user can also press Stop. From the first stop signal, tools have 60 seconds to finish. If that grace expires, the runner saves the original reason (stopped or timed out) and the chat accepts another message. The Celery task keeps its slot while Kuma drains tools it already started. Prefork can still kill the child at the task's 17-minute hard limit. On threads a tool can keep running, so its blocking calls need their own timeouts.

Some adapter hooks have more than one caller context. on_run_not_started can run in an HTTP request that cancels a queued run or fails to enqueue it, or in the sweep_chat_runs maintenance task. That task also calls on_run_lost for expired runs. serialize_outcome is used by both HTTP reads and worker completion. The ChatRunType docstrings describe each hook's callers.

Adding a product

Register a ChatRunType in chat_run_type_registry during app startup:

class NotesChatRunType(ChatRunType):
    type = "notes"

    def can_read(self, user, run):  # see output and status
        return run.created_by_id == user.id

    def can_cancel(self, user, run):  # press Stop
        return run.created_by_id == user.id

    def can_follow_chat(self, user, chat_id):  # subscribe to lifecycle updates
        return NotesChat.objects.filter(id=chat_id, user=user).exists()

    async def execute(self, run, journal):
        text = ""
        async for token in ask_model(run.context["prompt"]):
            if journal.stop_requested.is_set():
                return ChatRunOutcome(ChatRunStatus.CANCELLED, result=text)
            text += token
            journal.emit_text(text)
        return ChatRunOutcome(ChatRunStatus.COMPLETED, result=text)

    def persist_outcome(self, run, outcome):
        message = NotesMessage.objects.create(chat_id=run.chat_id, text=outcome.result)
        return {"id": message.id, "text": message.text}

The three access checks are separate on purpose: someone may be allowed to see a run but not stop it. can_follow_chat checks websocket subscriptions; HTTP output followers use can_read.

Each run type limits only its own active runs. By default a user can have two at once; override get_max_active_runs_per_user(user) (None for no limit) or get_max_active_runs_per_workspace(workspace) to change that. At a limit, create_run raises ChatRunLimitReached, which the product turns into its own error.

Agent replies default to unlimited at both levels, so an install has no cap until an operator sets one. Configure BASEROW_AGENT_MAX_ACTIVE_RUNS_PER_USER and BASEROW_AGENT_MAX_ACTIVE_RUNS_PER_WORKSPACE to bound queued or running replies; unset, empty, 0 or a negative value means unlimited. The user cap counts across workspaces, and both caps are enforced in the database across backend processes. Opening another tab or disconnecting does not change the active-run count. At either limit, a new Agent send returns HTTP 429 ERROR_AGENT_BUSY without saving its messages.

A plugin can replace the registered AgentChatRunType and override get_max_active_runs_per_workspace(workspace) to select a workspace's cap from its plan, or get_max_active_runs_per_user(user) to set one user's cap across workspaces, for example from a per-user setting it stores. Both methods run synchronously when a new run is created. Return None for unlimited or 0 to refuse new runs; lowering a limit does not stop runs already going.

Core serves GET /api/chat-runs/<run_id>/stream/?cursor= for every run type. Its URL name is registered with the stream router through register_async_stream_path from baserow.config.streaming, and deployments route /api/chat-runs/ to the ASGI service. A product needs no stream path or view of its own. The endpoint relays every buffered event to anyone can_read admits, so a run type must only emit output that every reader of the run may see.

On the frontend, register a ChatRunType from chatRuns/chatRunType.js. By default it reads that stream and handles the resume baseline: the record that rebuilds the output before the cursor a follower resumes from. The product only folds its own events into what the UI shows, in foldEvent.

To follow runs that other tabs start, a product subscribes to one chat per run type with chatRuns/subscribeToChat and releases it with chatRuns/unsubscribeFromChat. Websocket lifecycle messages are not replayed, so the store calls the type's refreshChat when the subscription is confirmed again after a reconnect. While no output arrives, an open stream sends a run.heartbeat record about every 5 seconds; the store restarts a stream that sends nothing for 15 seconds.

When things go wrong

The engine never runs an answer twice. Tools may already have changed data, so a second run could repeat those changes.

What happens Result
Redis loses the output Followers can't replay. The browser uses the saved status and result instead. The worker carries on.
The worker dies mid-answer The run is marked lost. on_run_lost gets the output Redis still has, if complete, so the product can save it.
The run never started on_run_not_started saves a result for it.
Saving the answer failed ensure_interruption_warning stores an "answer was interrupted" warning in the product's conversation.
The run is 7 days old It is deleted. Incomplete runs first get one more ensure_interruption_warning call, which may fail.

The buffer always keeps the output from the very start. If a run produces more output than the buffer allows, replay is turned off for that run. Trimming the start would rebuild a wrong answer in the browser.

Every event has a cursor. If the buffer is lost and recreated, old cursors are rejected instead of silently skipping output. Events sent again during a replay have replayed: true, so the product must not repeat side effects for them, such as navigating the page.

How streaming responses are served

Django's normal ASGI handler keeps a request-specific thread context until the response ends. A follower can last minutes, so it uses a separate path:

flowchart TD
    A[Request arrives] --> B{Registered streaming URL?}
    B -->|No| C[Existing HTTP application]
    B -->|Yes| D[Read request body]
    D --> E{Stream slot available?}
    E -->|No| F[Return HTTP 503]
    E -->|Yes| G[Django checks permissions and runs the view]
    G --> H[Deliver output asynchronously]
    H --> I[Finished or disconnected: clean up]
  • The body is read using Django's normal upload handling before a slot is taken. A slow upload never holds a slot.
  • BASEROW_ASGI_HTTP_MAX_CONCURRENT_STREAMS sets the slots per backend process. Each follower uses one Redis connection while it waits, so the Redis read pool has the same size. For example, 4 processes with 100 slots each can open 400 Redis read connections.
  • Followers don't count against BASEROW_ASGI_HTTP_MAX_CONCURRENCY, so long answers never block normal requests.
  • Redis calls time out after 1 s, including the DNS lookup for a new connection, and every run in the worker opens new connections. Where lookups can stall, as in some Kubernetes clusters, use a node-local DNS cache: a run whose first lookup is slow streams no live output, only its final answer.