Skip to content

fix(client): send CLOSE when a subscription handle is closed - #566

Merged
tcheeric merged 1 commit into
developfrom
fix/subscription-close-sends-close
Nov 25, 2026
Merged

tcheeric merged 1 commit into
developfrom
fix/subscription-close-sends-close

Conversation

@tcheeric

Copy link
Copy Markdown
Owner

Summary

NostrRelayClient's subscription handle only removed the local listener and never sent CLOSE, despite RelayConnection.subscribe documenting that the handle "cancels the subscription when closed". Relays (strfry, limit 200) accumulated open REQs and started refusing them with too many concurrent REQs (took down imani-support-agent's support-snapshot service, worked around in imani-support-agent#13).

Changes

  • Typed subscribe(ReqMessage, ...) handle: removes the listener, then sends ["CLOSE", id] fire-and-forget via the gated session write. Idempotent (second close is a no-op). Never waits for a reply. Failure or a closed session is only logged at debug.
  • The CLOSE does not claim the single in-flight send() slot. That was the concern in the old RelaySubscription.close() javadoc ("sending one would consume the slot the next subscription needs"). In addition, the relay's CLOSED reply to a client-initiated CLOSE is now kept from completing an unrelated in-flight send() (it is still routed by id, to the already-removed listener, so nobody sees it).
  • Raw-JSON subscribe(String, ...) is unchanged: no id is parsed, so no CLOSE is sent (guessing an id from unvalidated JSON could close another subscription). Documented inline.
  • RelaySubscription.close() javadoc updated. CHANGELOG [Unreleased] entry.

Tests

New NostrRelayClientSubscriptionCloseTest:

  • closing a handle twice sends exactly one ["CLOSE","sub-1"]
  • raw-JSON handle sends no CLOSE
  • closing after the session closed does not throw
  • subscribe/close/subscribe on one connection: second subscription still gets its EVENT and EOSE, closed one gets nothing, frame sequence is REQ, CLOSE, REQ, CLOSE

mvn -pl nostr-java-client test: Tests run: 89, Failures: 0, Errors: 0, Skipped: 0. Full mvn -q verify not run.

Follow-up

After this ships in a release, imani-support-agent should bump nostr-java and remove the RelayPoolGateway.sendClose workaround from PR #13 (otherwise it sends a duplicate CLOSE, harmless but redundant).

Closing the handle from NostrRelayClient.subscribe(ReqMessage, ...) now drops the
listener and sends ["CLOSE", id] once, fire-and-forget. Relays no longer
accumulate open REQs until they refuse new ones. The relay's CLOSED reply to a
client-initiated CLOSE does not complete an in-flight send(). Raw-JSON
subscriptions are unchanged (no parsed id).
@tcheeric
tcheeric merged commit f093914 into develop Nov 25, 2026
5 checks passed
This was referenced Nov 25, 2026
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.

1 participant