Skip to content

Fix: debt: SyncScheduler request pump can strand queued requests - #109

Open
goransh-walia wants to merge 1 commit into
goatplatform:mainfrom
goransh-walia:fix/issue-102-611
Open

goransh-walia wants to merge 1 commit into
goatplatform:mainfrom
goransh-walia:fix/issue-102-611

Conversation

@goransh-walia

Copy link
Copy Markdown

Fixes #102

Summary

This PR prevents SyncScheduler from 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:
    • Added _inFlightRequests to track requests undergoing fetch and processing.
    • Updated close() to reject all _inFlightRequests with serviceUnavailable().
    • Wrapped request payload serialization in a try/catch block to reject the batch on encoding errors.
    • Wrapped per-request response decoding in a try/catch block to prevent single decode failures from rejecting other requests in the batch.
    • Added a finally block to cleanup _inFlightRequests and reset _fetchInProgress when batch processing finishes.

Testing

The full test suite was not run locally, so I'm relying on CI to confirm.

AI-assisted.

@ofriw ofriw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 SyncScheduler with generateSession(), a TrustPool, and a DataRegistry.
  • 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.ts and call its setup from tests/test-registry.ts.
  • Call close() in finally, 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 SerializeError for the serialization catch at :184-193.
  • Use DecodeError or ResponseDecodeError for the response catches.
  • Add the new label to OperationalError in logging/errors.ts. Without this step, deno check fails.

Done when

  • One rejection path remains.
  • No non-ServiceUnavailable error is silent.
  • deno check mod.ts and deno lint pass.

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.

This branch has not been deployed

No deployments
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.

debt: SyncScheduler request pump can strand queued requests

2 participants