Shared sync worker keeps a background tab's database connection (`getRandomWrappedPort`), slowing writes in the visible tab until that tab closes
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- javascript, sqlite, typescript
Hướng nghiên cứu
Start with packages/web/src/worker/sync/SharedSyncImplementation.ts, especially openInternalDB() and its getRandomWrappedPort() call, then trace handleClientClosed, markConnectionMayHaveChanged, and the connection-selection behavior used by getLastWrappedPort. Reproduce with two same-origin tabs using the listed shared-worker and OPFSCoopSyncVFS setup; done means the shared connection and connector calls use the in-use tab without the background-tab write contention described.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Versions: @powersync/web 2.4.1 (same in 2.0.0), @powersync/common 2.3.0, @powersync/shared-internals 1.3.0, @journeyapps/wa-sqlite 2.0.6. WASQLiteVFS.OPFSCoopSyncVFS, enableMultiTabs: true, shared sync worker (@powersync/web/bundled_worker). Chrome 153 and Safari 26 / WebKit (also reproduced with Playwright Chromium).
Steps to reproduce
- Open the app in tab A,
db.connect(...). - Open the same app in tab B (same origin),
db.connect(...). - Reload tab A.
- In any tab, run
(await navigator.locks.query()).heldand match theclientIdof theshared-connection-<dbFilename>-…lock against each tab's own probe lock.
Expected: the shared sync worker reads and writes through the connection of the tab that is in use (visible/focused), or at least moves to a newly connected tab, as it already does for uploadCrud and fetchCredentials (getLastWrappedPort).
Actual: when A's port closes, the worker reopens its internal connection right away (handleClientClosed → triggerCrudUpload / markConnectionMayHaveChanged). At that moment B is the only port, so openInternalDB → getRandomWrappedPort() picks B. The worker then keeps B's connection for as long as B lives. In our measurements the connection stayed with the background tab after 4 of 4 reloads. Even without that timing, the pick is random among all non-closing ports.
Impact: with OPFSCoopSyncVFS every write in the visible tab now contends with a second connection hosted by another tab's dedicated worker. That tab is often a background tab and may be throttled. In our app, creating a card went from 129–138 ms (single tab) to 313–364 ms median (2.3–2.7×) until the background tab was closed.
Code: packages/web/src/worker/sync/SharedSyncImplementation.ts — openInternalDB() calls getRandomWrappedPort(). The connection is only replaced from the tab-closed listener.
Related: DatabaseClient.shareConnection() holds its shared-connection-* lock until the tab's own database closes. When the worker closes the shared connection earlier, the lock stays held: the tab can't be frozen, and the lock no longer tells who hosts the connection.
What we do locally (pnpm patch, happy to open a PR):
- Tabs tell the worker whether they are in use (our app decides this with visibility/focus).
- The worker opens its connection through the most recently activated tab. After the active tab's port closes, it waits a few seconds for an active tab to reconnect instead of settling on a background one.
- The worker moves an open connection to a newly activated tab: it drains the old connection, closes it and calls
markConnectionMayHaveChanged. Each shared connection gets its own releasable lock. - Connector calls (
uploadCrud,fetchCredentials) also go to the active tab instead of the last connected one (getLastWrappedPort). Otherwise a background tab opened later runsuploadDataagainst its own connection, which brings back the same contention: 252–298 ms vs 135–178 ms in our measurements.
A minimal repro (two tabs, no backend needed — the connection is borrowed on setParams) is available on request.
Found while building LayerPx — a local-first tree planner on PowerSync.
- Ngôn ngữ chính
- TypeScript
- Star
- 729
- Fork
- 96
- Merge trung bình
- 2 ngày 5 giờ
- Pull request đã merge (30 ngày)
- 24
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của powersync-ja/powersync-js
-
Sync loop stops with a TypeError when an iteration rejects with no value after a connection changeĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
powersync-ja/powersync-js#1163 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
powersync-ja/powersync-js#1159 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Differential watched queries ignore pure reorders (same rows, new ORDER BY) so stale order is servedĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
powersync-ja/powersync-js#1156 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
powersync-ja/powersync-js#1165 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 54/100
powersync-ja/powersync-js#1164 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của powersync-ja/powersync-js
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
awesome-dsh-plugin/awesome-dsh-plugin#9184 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
farion1231/cc-switch#8829 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[core-client] Remove unused hand-maintained transport helpers after the definition-table cutoverĐang mởdoc-audit enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100