Repository navigation
Fix: debt: SyncScheduler request pump can strand queued requests - #109
goransh-walia wants to merge 1 commit into
Conversation
ofriw
left a comment
There was a problem hiding this comment.
Note: This review was generated by an AI agent. If you'd like to talk with other humans, drop by our Discord!
PR #109 — SyncScheduler Request Pump
PR: #109
Head: dd69d74 · Base: 2be88b3 · Changed file: net/sync-scheduler.ts
Decision: Changes requested. Do not merge.
B1 — The fixes have no test coverage
The PR repairs three request-settlement paths, but no test runs them. tests/ has no reference to SyncScheduler, so the suite cannot show that the fixes work or guard against a pump refactor. Issue #102 also asks for a test that injects an encode failure and a partial decode failure.
Required test cases
| Case | Setup | Expected result |
|---|---|---|
| Serialization throw | One request in a batch throws in JSONCyclicalEncoder.serialize() |
Every request in the batch rejects. No request promise stays pending. |
| Partial decode failure | One response in a batch cannot decode. The other responses are valid. | The failed request rejects. The valid requests resolve. The loop continues to the next request. |
| Close during flight | Call close() while the batch fetch is pending. |
The in-flight requests reject with ServiceUnavailable. The queued requests also reject. |
Test setup
- Create a real
SyncSchedulerwithgenerateSession(), aTrustPool, and aDataRegistry. - Use a local HTTP server for
/batch-sync. Keep the response open for the close case. - Do not mock the scheduler. Exercise the production pump.
- Add
tests/sync-scheduler.test.tsand call its setup fromtests/test-registry.ts. - Call
close()infinally, because the constructor starts a 200 ms interval.
Done when
deno task test --test="SyncScheduler"passes.- If you revert a fix, at least one new test fails.
Code under test: net/sync-scheduler.ts:103-122, :177-193, :233-290.
B2 — Error handling repeats, and the outer catch is silent
The per-request catch at net/sync-scheduler.ts:260-273 and the outer parse catch at :276-290 hold the same log-and-reject logic. The outermost catch at :291-292 rejects every request with no log record. An error that escapes the inner handlers, such as a throw from log() itself, is therefore silent. Both response catches also label decode and parse errors as SerializeError (:266, :282).
Required change
- Extract one helper. The helper writes one log record for errors other than
ServiceUnavailable. It then rejects the given requests. - Use the helper in the per-request, outer parse, and outermost catches.
- Keep
SerializeErrorfor the serialization catch at:184-193. - Use
DecodeErrororResponseDecodeErrorfor the response catches. - Add the new label to
OperationalErrorinlogging/errors.ts. Without this step,deno checkfails.
Done when
- One rejection path remains.
- No non-
ServiceUnavailableerror is silent. deno check mod.tsanddeno lintpass.
B3 — Restore the final newline
net/sync-scheduler.ts ends at line 299 with } and no newline. The base revision ends with a newline. deno fmt --check fails on the file.
Add the newline. Do not add other formatting changes.
Fixes #102
Summary
This PR prevents
SyncSchedulerfrom stranding request promises when serialization or response decoding fails, and ensures in-flight requests settle when the scheduler is closed.Root cause
Requests were dequeued prior to JSON serialization without error handling, causing any serialization throw to leave request promises unsettled. Additionally, individual response decoding errors within a batch aborted the loop and rejected the remaining requests, while
close()only cleared pending queued requests without rejecting requests currently in flight.Changes
net/sync-scheduler.ts:_inFlightRequeststo track requests undergoing fetch and processing.close()to reject all_inFlightRequestswithserviceUnavailable().try/catchblock to reject the batch on encoding errors.try/catchblock to prevent single decode failures from rejecting other requests in the batch.finallyblock to cleanup_inFlightRequestsand reset_fetchInProgresswhen batch processing finishes.Testing
The full test suite was not run locally, so I'm relying on CI to confirm.
AI-assisted.