Skip to content

fix(canvas): buffer runtime messages posted before the port connects - #78243

Merged
trunk-io[bot] merged 3 commits into
masterfrom
fix/canvas-runtime-port-buffer
Aug 14, 2026
Merged

fix(canvas): buffer runtime messages posted before the port connects#78243
trunk-io[bot] merged 3 commits into
masterfrom
fix/canvas-runtime-port-buffer

Conversation

@jakesciotto

@jakesciotto jakesciotto commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Problem

  • A canvas app that queries while it mounts hangs for 30 seconds, then fails with "Canvas request timed out".
  • The host delivers the MessagePort only after the iframe's load event, which fires after the app's module scripts ran.
  • port?.postMessage(...) drops anything posted before that.

Changes

  • The runtime queues messages posted before the port exists, bounded at 256, and flushes them when the port connects.
  • A request whose 30-second timeout fires while still queued leaves the queue, so a late connect cannot run work the caller saw fail.

How did you test this code?

  • New test_runtime_flushes_data_requests_queued_before_the_port_connects runs the built runtime under node with a fake port and fake timers.
  • The test fails against a runtime that drops pre-connect requests, and against one that flushes an expired request.
  • No existing test exercised the pre-connect window.

Automatic notifications

  • Publish to changelog?

Docs update

None.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • I (actually Claude Code) authored this while investigating slow canvas cold loads in the desktop app.
  • On review I rebased the change onto master's themed runtime and added the expired-request drop that Greptile flagged.
  • The harness now stubs the document APIs the bundled selection and highlight runtimes touch at load.
  • Skills invoked: /writing-tests, /writing-code-comments, /writing-pr-descriptions.
  • The queue is bounded so a canvas erroring in a loop before connect cannot grow it without limit.

Created with PostHog Code

@jakesciotto jakesciotto self-assigned this Aug 5, 2026
@trunk-io

trunk-io Bot commented Aug 5, 2026

Copy link
Copy Markdown

😎 Merged successfully - details.

@trunk-io

trunk-io Bot commented Aug 5, 2026

Copy link
Copy Markdown

Static BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

@jakesciotto
jakesciotto marked this pull request as ready for review August 5, 2026 13:54
@pr-assigner-resolver-posthog
pr-assigner-resolver-posthog Bot requested a review from a team August 5, 2026 13:54
@greptile-apps

greptile-apps Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
### Issue 1
products/canvas/packages/canvas_builder/build.mjs:30
**Expired requests still execute**

When the MessagePort connects more than 30 seconds after a pre-connect `ph.capture` call, the timeout removes only the pending promise entry while leaving its payload queued, so the subsequent flush records the event after the caller has already received a timeout; expired queries and insight requests are likewise executed with their responses ignored.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "fix(canvas): buffer runtime messages pos..." | Re-trigger Greptile

Comment thread products/canvas/packages/canvas_builder/build.mjs Outdated
@jakesciotto
jakesciotto force-pushed the fix/canvas-runtime-port-buffer branch from e5ffb2e to 6db7118 Compare August 7, 2026 02:26

Copy link
Copy Markdown
Contributor Author

Addressed the Greptile finding in 6db7118: a request whose timeout fires while still queued is now removed from the queue, so a late connect no longer executes expired captures or queries. The harness test covers this case (it fails with the splice removed). Also rebased onto master's themed runtime to clear the conflict.

@github-actions github-actions Bot added the feature/desktop Feature Tag: Desktop label Aug 7, 2026
@jakesciotto
jakesciotto force-pushed the fix/canvas-runtime-port-buffer branch from 6db7118 to 4e13464 Compare August 7, 2026 02:34
@jakesciotto
jakesciotto enabled auto-merge (squash) August 7, 2026 04:56
jakesciotto and others added 3 commits August 12, 2026 13:05
The host delivers the MessagePort only after the artifact iframe's load
event, which fires after the app's module scripts ran. A ph.query issued
during mount was silently dropped by port?.postMessage and rejected 30s
later with "Canvas request timed out". The runtime now queues
pre-connect messages (bounded) and flushes them when the port arrives.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
A pre-connect ph.* request whose 30s timeout already rejected stayed in the queue, so a late-connecting host still executed it after the caller gave up. The timeout now removes the queued payload alongside the pending promise.

Generated-By: PostHog Code
Task-Id: 63fc8373-9e47-48f5-b6ba-23a48d562593
canvas-runtime.js now bundles the selection and highlight runtimes after the data bridge, and those touch document.addEventListener, createElement, head.appendChild, body, and MutationObserver at load. The flush-test harness only stubbed readyState, so the bundled file threw before any assertion ran.

Generated-By: PostHog Code
Task-Id: 63fc8373-9e47-48f5-b6ba-23a48d562593
@jakesciotto
jakesciotto force-pushed the fix/canvas-runtime-port-buffer branch from 781089f to 21a2041 Compare August 12, 2026 19:05
@k11kirky

Copy link
Copy Markdown
Contributor

/trunk merge

@trunk-io
trunk-io Bot merged commit 93368ad into master Aug 14, 2026
263 checks passed
@trunk-io
trunk-io Bot deleted the fix/canvas-runtime-port-buffer branch August 14, 2026 11:20
@deployment-status-posthog

deployment-status-posthog Bot commented Aug 14, 2026

Copy link
Copy Markdown

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-08-14 12:00 UTC Run
prod-us ✅ Deployed 2026-08-14 13:59 UTC Run
prod-eu ✅ Deployed 2026-08-14 12:29 UTC Run

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

Labels

feature/desktop Feature Tag: Desktop

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants