Skip to content

fix(workspace-server): tolerate not-ready DB in agent file activity path#2966

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-agent-file-activity-db-race
Draft

fix(workspace-server): tolerate not-ready DB in agent file activity path#2966
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-agent-file-activity-db-race

Conversation

@posthog

@posthog posthog Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Problem

An unhandled rejection — Database not initialized — call initialize() first — was firing in the workspace-server agent file-activity path (6 occurrences across 4 users, first seen 2026-06-28).

When the agent edits a file, AgentService.emitAgentFileActivityForCurrentBranch fires a fire-and-forget emit. The listener, WorkspaceService.handleAgentFileActivity, is async and its first statement does a synchronous DB read (workspaceRepo.findByTaskId), which throws from the DatabaseService.db getter during the startup/teardown window when the DB is closed (@preDestroy) or not yet initialized. Because the listener is async and TypedEventEmitter.emit discarded the returned promise, that synchronous throw became a rejected promise that escaped into error tracking as noise — it never broke a user-facing flow (branch association is best-effort observability).

Changes

  • Add DatabaseService.isInitialized() and have handleAgentFileActivity bail gracefully when the DB isn't ready, so the best-effort branch-association read no longer throws during start/shutdown.
  • Harden TypedEventEmitter.emit to swallow rejections from async listeners, so a throwing async listener can never become an unhandled rejection again (defense in depth).

How did you test this?

  • Added a unit test asserting handleAgentFileActivity returns without touching the repo when the DB is not initialized.
  • Added a unit test asserting emit does not surface a rejecting async listener as an unhandled rejection (and that later listeners still run).
  • Ran pnpm --filter @posthog/workspace-server exec vitest run workspace (57 passed) and the shared emitter suite (14 passed).
  • Typechecked @posthog/shared and @posthog/workspace-server; Biome clean on changed files.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code from an inbox report.

The fire-and-forget `emitAgentFileActivityForCurrentBranch` emit lands on the
async `WorkspaceService.handleAgentFileActivity` listener, whose first statement
is a synchronous repo read. During the startup/teardown window (DB closed via
`@preDestroy` or not yet initialized) the `DatabaseService.db` getter throws.
Because the listener is async and `TypedEventEmitter.emit` discarded the returned
promise, that throw became an unhandled rejection that polluted error tracking.

- Add `DatabaseService.isInitialized()` and bail gracefully from
  `handleAgentFileActivity` when the DB isn't ready — branch association is
  best-effort.
- Harden `TypedEventEmitter.emit` to swallow rejections from async listeners so
  a throwing listener can never become an unhandled rejection again.

Generated-By: PostHog Code
Task-Id: c17e519a-cfe2-412c-856e-17e8870407c5
@github-actions

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 35af854.

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.

0 participants