Skip to content

Add ability to unsubscribe - #44

Merged
callumforrester merged 1 commit into
mainfrom
subscribe-race-condition
Oct 14, 2025
Merged

callumforrester merged 1 commit into
mainfrom
subscribe-race-condition

Conversation

@callumforrester

@callumforrester callumforrester commented Oct 13, 2025 •

Copy link
Copy Markdown
Contributor

Add ability to unsubscribe from queues and topics so clients can clean up old subscriptions to avoid race conditions.

This preserves the extant and implicit behaviour of resubscribing on reconnection. We could discuss whether we actually want bluesky-stomp to do this, although I suggest that's for later (and a major version change).

CI broken pending #43

@codecov

codecov Bot commented Oct 13, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.68%. Comparing base (d094d8c) to head (615f9df).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #44      +/-   ##
==========================================
+ Coverage   94.50%   94.68%   +0.17%     
==========================================
  Files           6        6              
  Lines         273      282       +9     
==========================================
+ Hits          258      267       +9     
  Misses         15       15              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@callumforrester
callumforrester force-pushed the subscribe-race-condition branch from 81d4b74 to b120e14 Compare October 13, 2025 14:51
@callumforrester callumforrester changed the title Add ability to unscubscribe Add ability to unsubscribe Oct 13, 2025
@callumforrester
callumforrester force-pushed the subscribe-race-condition branch from b120e14 to 5334140 Compare October 13, 2025 15:03
callumforrester added a commit to DiamondLightSource/blueapi that referenced this pull request Oct 13, 2025
Pending DiamondLightSource/bluesky-stomp#44

Unsubsribe from STOMP messages after use to prevent a race condition. The client already disconnects from the stomp broker after its context exits, but any subscriptions are resubscribed when the client reconnects, they need to be explicitly unsubscribed.
callumforrester added a commit to DiamondLightSource/blueapi that referenced this pull request Oct 13, 2025
Pending DiamondLightSource/bluesky-stomp#44

Unsubsribe from STOMP messages after use to prevent a race condition. The client already disconnects from the stomp broker after its context exits, but any subscriptions are resubscribed when the client reconnects, they need to be explicitly unsubscribed.
Comment on lines +342 to +344
del self._subscriptions[
sub_id
] # Ensure that client won't re-subscribe upon re-connection

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
del self._subscriptions[
sub_id
] # Ensure that client won't re-subscribe upon re-connection
del self._subscriptions[
sub_id
] # Ensure that client won't re-subscribe upon re-connection

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you subscribe without connecting it caches the subscription for after connection.

# Caches callback for when connected to the broker
client.subscribe(my_queue, callback_1)

# Actually does the I/O with broker to subscribe for callback_1
client.connect()

# Immediately does I/O with broker to subscribe for callback_2
client.subscribe(my_queue, callback_2)  

So we want unsubscribe to mirror that behaviour

# Caches both callbacks for later
s1 = client.subscribe(my_queue, callback_1)
s2 = client.subscribe(my_queue, callback_2)

# Removes first cached callback
client.unsubscribe(s1)

# Does I/O for second subscription
client.connect()

# Does I/O for unsubscribing for second callback
client.unsubscribe(s2)

If we do as you suggest that then both s1 and s2 will be cached but the first unsubscribe call will be a no-op so connect() will still do both subscriptions, which is confusing.

@callumforrester
callumforrester merged commit 1a020f3 into main Oct 14, 2025
@callumforrester
callumforrester deleted the subscribe-race-condition branch October 14, 2025 12:44
callumforrester added a commit to DiamondLightSource/blueapi that referenced this pull request Oct 15, 2025
Pending DiamondLightSource/bluesky-stomp#44

Unsubsribe from STOMP messages after use to prevent a race condition.
The client already disconnects from the stomp broker after its context
exits, but any subscriptions are resubscribed when the client
reconnects, they need to be explicitly unsubscribed.
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.

2 participants