Skip to content

Commit 0ed4bb5

Browse files
Make Subscription#Next return null/false when underlying channel is closed
1 parent d8daaa2 commit 0ed4bb5

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

projects/client/RabbitMQ.Client/src/client/api/IModel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,15 @@ public interface IModel: IDisposable
126126
///otherwise.</summary>
127127
ShutdownEventArgs CloseReason { get; }
128128

129-
///<summary>Returns true if the session is still in a state
129+
///<summary>Returns true if the model is still in a state
130130
///where it can be used. Identical to checking if CloseReason
131131
///== null.</summary>
132132
bool IsOpen { get; }
133133

134+
///<summary>Returns true if the model is no longer in a state
135+
///where it can be used.</summary>
136+
bool IsClosed { get; }
137+
134138
///<summary>When in confirm mode, return the sequence number
135139
///of the next message to be published.</summary>
136140
ulong NextPublishSeqNo { get; }

projects/client/RabbitMQ.Client/src/client/impl/ModelBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,15 @@ public bool IsOpen
522522
}
523523
}
524524

525+
public bool IsClosed
526+
{
527+
get
528+
{
529+
return !IsOpen;
530+
}
531+
}
532+
533+
525534
public ulong NextPublishSeqNo
526535
{
527536
get

projects/client/RabbitMQ.Client/src/client/messagepatterns/Subscription.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public BasicDeliverEventArgs Next()
212212
// from under us by the operation of Close() from
213213
// another thread.
214214
QueueingBasicConsumer consumer = m_consumer;
215-
if (consumer == null) {
215+
if (consumer == null || m_model.IsClosed) {
216216
// Closed!
217217
m_latestEvent = null;
218218
} else {
@@ -275,9 +275,11 @@ public bool Next(int millisecondsTimeout, out BasicDeliverEventArgs result)
275275
// from under us by the operation of Close() from
276276
// another thread.
277277
QueueingBasicConsumer consumer = m_consumer;
278-
if (consumer == null) {
278+
if (consumer == null || m_model.IsClosed) {
279279
// Closed!
280280
m_latestEvent = null;
281+
result = null;
282+
return false;
281283
} else {
282284
BasicDeliverEventArgs qValue;
283285
if (!consumer.Queue.Dequeue(millisecondsTimeout, out qValue)) {

0 commit comments

Comments
 (0)