@@ -98,10 +98,10 @@ public class Subscription: IEnumerable, IEnumerator, IDisposable {
98
98
public IModel Model { get { return m_model ; } }
99
99
100
100
protected string m_queueName ;
101
- protected QueueingBasicConsumer m_consumer ;
101
+ protected volatile QueueingBasicConsumer m_consumer ;
102
102
protected string m_consumerTag ;
103
103
protected bool m_noAck ;
104
- protected bool m_shouldDelete ;
104
+ protected volatile bool m_shouldDelete ;
105
105
106
106
///<summary>Retrieve the queue name we have subscribed to. May
107
107
///be a server-generated name, depending on how the
@@ -219,9 +219,18 @@ public Subscription(IModel model, string queueName, bool noAck)
219
219
public void Close ( )
220
220
{
221
221
try {
222
- if ( m_consumer != null ) {
222
+ bool shouldCancelConsumer = false ;
223
+ lock ( this ) {
224
+ if ( m_consumer != null ) {
225
+ shouldCancelConsumer = true ;
226
+ m_consumer = null ;
227
+ }
228
+ }
229
+ if ( shouldCancelConsumer ) {
223
230
m_model . BasicCancel ( m_consumerTag ) ;
231
+ m_consumerTag = null ;
224
232
}
233
+
225
234
if ( m_shouldDelete ) {
226
235
m_shouldDelete = false ;
227
236
// We set m_shouldDelete false before attempting
@@ -232,8 +241,6 @@ public void Close()
232
241
} catch ( OperationInterruptedException ) {
233
242
// We don't mind, here.
234
243
}
235
- m_consumer = null ;
236
- m_consumerTag = null ;
237
244
}
238
245
239
246
///<summary>Causes the queue to which we have subscribed to be
@@ -319,9 +326,10 @@ public BasicDeliverEventArgs Next()
319
326
try {
320
327
if ( m_consumer == null ) {
321
328
// Closed!
322
- throw new InvalidOperationException ( ) ;
329
+ m_latestEvent = null ;
330
+ } else {
331
+ m_latestEvent = ( BasicDeliverEventArgs ) m_consumer . Queue . Dequeue ( ) ;
323
332
}
324
- m_latestEvent = ( BasicDeliverEventArgs ) m_consumer . Queue . Dequeue ( ) ;
325
333
} catch ( EndOfStreamException ) {
326
334
m_latestEvent = null ;
327
335
}
@@ -377,14 +385,15 @@ public bool Next(int millisecondsTimeout, out BasicDeliverEventArgs result)
377
385
try {
378
386
if ( m_consumer == null ) {
379
387
// Closed!
380
- throw new InvalidOperationException ( ) ;
381
- }
382
- object qValue ;
383
- if ( ! m_consumer . Queue . Dequeue ( millisecondsTimeout , out qValue ) ) {
384
- result = null ;
385
- return false ;
388
+ m_latestEvent = null ;
389
+ } else {
390
+ object qValue ;
391
+ if ( ! m_consumer . Queue . Dequeue ( millisecondsTimeout , out qValue ) ) {
392
+ result = null ;
393
+ return false ;
394
+ }
395
+ m_latestEvent = ( BasicDeliverEventArgs ) qValue ;
386
396
}
387
- m_latestEvent = ( BasicDeliverEventArgs ) qValue ;
388
397
} catch ( EndOfStreamException ) {
389
398
m_latestEvent = null ;
390
399
}
0 commit comments