Skip to content

Improve tab close detection for shared sync worker#425

Open
simolus3 wants to merge 4 commits into
mainfrom
worker-channel-detect-end
Open

Improve tab close detection for shared sync worker#425
simolus3 wants to merge 4 commits into
mainfrom
worker-channel-detect-end

Conversation

@simolus3
Copy link
Copy Markdown
Contributor

@simolus3 simolus3 commented Jun 3, 2026

Currently, sync workers detect closed tabs by listening on database.closed for a database backed by sqlite3_web (which internally uses navigator locks to detect closed tabs).

This kind of works, but can lead to deadlocks when a tab is closed while it's serving a request (like a fetchCredentials or requestDatabase call) that would never complete.

To improve reliability, this adopts the usual navigator locks pattern to our WorkerCommunicationChannel as well:

  1. Before requesting a sync iteration, tabs acquire a randomly-named lock, and they include that lock name in the request.
  2. Like before, the shared sync worker will mark a tab as closed once this lock is available to it. But starting from this PR, it also throws for all pending requests (they would never complete before).

I've also added a small test for the shared sync worker in sync_worker_test.dart. It emulates a shared worker by manually passing ports from a MessageChannel to it. I've also tested this in a demo app (and actually found an issue in sqlite3_web, I'll fix that too).

@simolus3 simolus3 requested a review from Chriztiaan June 3, 2026 11:48
@simolus3 simolus3 marked this pull request as ready for review June 3, 2026 11:48
@simolus3 simolus3 mentioned this pull request Jun 3, 2026
3 tasks
@Chriztiaan
Copy link
Copy Markdown
Contributor

Chriztiaan commented Jun 3, 2026

I found the following issue which I am not sure if it's a regression:

Launch supabase-todolist demo for web in two chrome tabs. Both show connected state, changes to the source database sync down to both tabs. I refresh the first tab, it never reaches a connected state and the second tab still shows connected. Changes to the source database don't sync down to either tab. If I close the first tab and refresh the second tab the data does then sync down.

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.

2 participants