fix: pairing handshake so the page and relay cannot miss each other - #51
Merged
Conversation
The pairing page's inline script runs while the document is parsing; the content script hosting the relay runs at document_idle. The page posted once, before the relay existed, so the message went nowhere and the page sat on '正在联系扩展…' forever. The relay now announces itself on install and the page answers, plus the page keeps announcing for 20s in case either signal is missed, and says so plainly if nothing ever responds. Unit tests missed this because they controlled the ordering themselves — the relay was always installed first.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Found while verifying v0.0.57 against a real browser: the pairing page sat on 「正在联系扩展…」 forever and no session could ever connect.
Root cause is a timing race, provable from the build artifacts. The pairing page's inline
<script>runs while the document is parsing and posted its payload exactly once. The content script hosting the relay is declaredrun_at: document_idle— it installs after that. The page announced itself to nobody, and the message was gone.The relay now announces itself the moment it installs, and the page answers. The page also keeps announcing for 20 s in case either signal is missed, and finally says plainly that it could not reach the extension instead of spinning forever. Either ordering now works — including reloading the extension while the pairing page is already open.
Why the unit tests missed it: they installed the relay first and then posted, because the test controls the ordering. The one ordering that happens in a real browser was the one never exercised. Both halves of the handshake now have tests.
The 404s visible in the same console (
react-root,approval,rpc,preload-helper) are the in-page chat widget lazy-loading its React chunks and are unrelated to pairing —pairing-relay.ts-*.jsloaded fine.Test plan
pnpm typecheckclean;pnpm test1128 passing;pnpm buildclean/tmp), different port, connected in 1.5 s with no clickmine=true busy=false, B sees the same tab asbusy=true busy_label=~/code/caiji2