# Why is the flow in the HandleMessageEvent sometimes null?

**URL:** <https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288>\
**Category:** General Discussions\
**Created:** [June 1, 2022, 1:37pm UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288 "2022-06-01T13:37:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordi](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordi](https://community.solace.com/u/Jordi)\
**Post date:** [June 1, 2022, 1:37pm UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/1 "2022-06-01T13:37:50Z")

</div>

Hi,  
we implemented a Solace consumer using the .NET API following the guidelines ( [Persistence with Queues](https://tutorials.solace.dev/dotnet/persistence-with-queues/) ).  
When runnung some tests we figured that sometimes messages were not properly acknowledged and removed from the queue. When debugging the HandleMessageEvent&nbsp;we found that apparently, sometimes, when \> calling  
Flow.Ack(message.ADMessageId)

> Flow was null, especially, when we created several instances of the Consumer. This also happens when we properly Dispose the Consumer before creating a new:  
> public void Dispose()  
> &nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Flow.Stop();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Flow.Dispose();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Session.Disconnect();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Session.Dispose();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Context.Dispose();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ContextFactory.Instance.Cleanup();  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> In the flow properties we only set  
> AckMode = MessageAckMode.ClientAck  
> Any idea, what might be the problem here?  
> Thanks, Jordi

---

<div class="post-metadata">

**Author:** ![TomF](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/tomf/32/1749_2.png) [@TomF](https://community.solace.com/u/TomF)\
**Post date:** [June 1, 2022, 2:02pm UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/2 "2022-06-01T14:02:31Z")

</div>

Hi @Jordi ,  
Are you seeing any FlowEvents ? Specifically, what are you doing in the HandleFlowEvent code? In the sample, it prints " Received Flow Event… " - are you seeing anything printed out like that? It could be that the flow has been disposed: you should see events, for instance that the flow is being closed.  
Do you check the flows are created properly? For instance, are you checking createFlow returns non-null?

---

<div class="post-metadata">

**Author:** ![Aaron](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/aaron/32/843_2.png) [@Aaron](https://community.solace.com/u/Aaron)\
**Post date:** [June 1, 2022, 3:35pm UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/3 "2022-06-01T15:35:41Z")

</div>

I’d also recommend not creating multiple Consumers, or re-creating them. Just create one-per-Session when first initializing your program and that’s it.

---

<div class="post-metadata">

**Author:** ![Jordi](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordi](https://community.solace.com/u/Jordi)\
**Post date:** [June 2, 2022, 7:27am UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/4 "2022-06-02T07:27:23Z")

</div>

Hi TomF and Aron,  
thanks for your comments. Yes, I check FlowEvents \> and flows seems to be properly created. I also create one flow per session. Here is what I do:  
The consumer looks the following:  
&nbsp;public SolaceConsumer(int id, string selectorName = null, string selectorValue = null)

> {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_id = id;´// for debugging  
> Console.WriteLine($“Starting consumer {\_id}”);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ContextFactoryProperties cfp = new ContextFactoryProperties()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SolClientLogLevel = SolLogLevel.Warning  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cfp.LogToConsoleError();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ContextFactory.Instance.Init(cfp);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X509Certificate2Collection certs = store.Certificates;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;store.Close();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SessionProperties sessionProps = new SessionProperties()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Host = Resources.JNDI\_PROVIDER\_URL,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VPNName = Resources.SOLACE\_JMS\_VPN,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReapplySubscriptions = true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserName = null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReconnectRetries = 5,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SSLValidateCertificate = true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SSLValidateCertificateDate = true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AuthenticationScheme = AuthenticationSchemes.CLIENT\_CERTIFICATE,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SSLClientCertificateFile = Resources.CERTIFICATE\_FILE,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SSLClientPrivateKeyFile = Resources.CERTIFICATE\_KEY,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SSLTrustStore = certs  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_context = ContextFactory.Instance.CreateContext(new ContextProperties(), null);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_session = \_context.CreateSession(sessionProps, MessageEventHandler, null);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IQueue queue = ContextFactory.Instance.CreateQueue(Resources.QUEUE);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FlowProperties flowProperties = new FlowProperties()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AckMode = MessageAckMode.ClientAck  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!(selectorName is null || selectorValue is null)) flowProperties.Selector = $“{selectorName} = ‘{selectorValue}’”;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReturnCode returnCode = \_session.Connect();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (returnCode == ReturnCode.SOLCLIENT\_OK)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_flow = \_session.CreateFlow(flowProperties, queue, null, MessageEventHandler, HandleFlowEvent);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (Exception e)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(e.Message);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;public void Dispose()  
> &nbsp;&nbsp;&nbsp;&nbsp;{  
> Console.WriteLine($“Disposing consumer {\_id}”);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_flow.Stop();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_flow.Dispose();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_session.Disconnect();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_session.Dispose();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_context.Dispose();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ContextFactory.Instance.Cleanup();  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;private void HandleFlowEvent(object sender, FlowEventArgs args)  
> &nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine($“Consumer {\_id}: {args.Event.ToString()}&nbsp;{args.Info}”);  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;private void MessageEventHandler(object source, MessageEventArgs args)  
> &nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IMessage msg = args.Message;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string content = Encoding.ASCII.GetString(msg.BinaryAttachment ?? msg.XmlContent);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReceivedMessages.Add(new KeyValuePair\<string, IDestination\>(content, msg.Destination));  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (true)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (\_flow is null)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine($“Flow {\_id} is null”);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReturnCode returnCode = \_flow.Ack(msg.ADMessageId);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (returnCode == ReturnCode.SOLCLIENT\_OK) msg.Dispose();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;}  
> Then I test, e.g., that the selector works:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// write two messages to the queue first, one with and one without selector  
> // create a selective listener  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SolaceConsumer selectiveConsumer = new SolaceConsumer(1, selectorName, selectorValue);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// wait to receive and acknowledge message  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;await Task.Delay(TimeSpan.FromSeconds(2));  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// the selective listener picks up only one message  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.AreEqual(1, selectiveConsumer.ReceivedMessages.Count);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // close selective listener  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectiveConsumer.Dispose();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// write a message with selector  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.WriteMessage(“Message 3”, selectorName, selectorValue);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// create a non-selective listener  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SolaceConsumer nonSelectiveConsumer = new SolaceConsumer(2);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// wait to receive and acknowledge message  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;await Task.Delay(TimeSpan.FromSeconds(2));  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// the non-selective listener picks up all messages  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.AreEqual(2, nonSelectiveConsumer.ReceivedMessages.Count);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nonSelectiveConsumer.Dispose();  
> What I get as output on the console is, e.g.:  
> Starting consumer 1  
> Consumer 1: UpNotice&nbsp; OK  
> Disposing consumer 1  
> Consumer 1: ParentSessionDown&nbsp; Session for Flow disconnected  
> Starting consumer 2  
> Consumer 2: UpNotice&nbsp; OK  
> Flow 2 is null  
> Disposing consumer 2  
> Consumer 2: ParentSessionDown&nbsp; Session for Flow disconnected  
> For consumer 2 the flow becomes null and, therefore, the messages are not properly acknowledged. Sometimes the flow becomes null for the first consumer. Seems random.  
> I also get a warning:  
> &nbsp; &nbsp; Warning - FlowImpl: 09:10:18 | Client Application threw an exception in flow rx message callback | System.NullReferenceException: Object reference not set to an instance of an object.  
> &nbsp; &nbsp;at SolaceConsumer.MessageEventHandler(Object source, MessageEventArgs args) in C:.…\SolaceConsumer.cs:line 139  
> &nbsp; &nbsp;at SolaceSystems.Solclient.Messaging.Native.FlowImpl.Flow\_RxCallbackFuncV2(IntPtr opaqueFlow, IntPtr opaqueMessage, IntPtr user)  
> So, any idea, why the flow becomes null?

---

<div class="post-metadata">

**Author:** ![TomF](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/tomf/32/1749_2.png) [@TomF](https://community.solace.com/u/TomF)\
**Post date:** [June 6, 2022, 10:31am UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/5 "2022-06-06T10:31:44Z")

</div>

Hi @Jordi , I strongly suspect that your exception is the problem. The exception is thrown in the message receive delegate. I’m not 100% sure, but I’m confident that this will cause the API to shut down the flow.  
The reason is that the API knows the RX delegate is broken (because there has been an exception), so messages won’t be processed properly. To prevent endless exceptions, the flow is terminated. I haven’t tried this in .Net, but that’s what the JAva API does.

---

<div class="post-metadata">

**Author:** ![Jordi](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordi](https://community.solace.com/u/Jordi)\
**Post date:** [June 7, 2022, 12:47pm UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/6 "2022-06-07T12:47:10Z")

</div>

Hi TomT,  
I am not sure what you mean.  
In the MessageEventHandler I do not handle any exception. There are just conditional statements. On the contrary, an exception is thrown, when the flow becomes null. The exception in the constructor is never thrown.  
My hypothesis is that the Solace client has problems when multiple consumers (session, flows, etc) for the same endpoint are created and disposed in a short time. Maybe the context or session caches something and then the flow assignment become ambiguous.  
Usually, this is not the case I assume, only here in this test instance.  
best,  
Jörg

---

<div class="post-metadata">

**Author:** ![TomF](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/tomf/32/1749_2.png) [@TomF](https://community.solace.com/u/TomF)\
**Post date:** [June 10, 2022, 1:59pm UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/7 "2022-06-10T13:59:44Z")

</div>

@Jordi \> , what I mean is that I think your MessageEventHandler code is throwing an exception somewhere, and you’re not catching it. That’s what the line:  
Client Application threw an exception in flow rx message callback   
in the log means.

---

<div class="post-metadata">

**Author:** ![Jordi](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordi](https://community.solace.com/u/Jordi)\
**Post date:** [June 10, 2022, 4:08pm UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/8 "2022-06-10T16:08:31Z")

</div>

When I put the whole message handler in a try-catch block, the only exception catched is when the flow becomes null and I try to acknowledge the message.

---

<div class="post-metadata">

**Author:** ![TomF](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/tomf/32/1749_2.png) [@TomF](https://community.solace.com/u/TomF)\
**Post date:** [June 13, 2022, 10:11am UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/9 "2022-06-13T10:11:03Z")

</div>

Hi @Jordi , ah, I see! The exception is thrown because the flow is null.  
Back to @Aaron 's comment, your .dispose() is rather brutal. Not only are you closing the flow, you are also disconnecting (and disposing of) the session. Unless you have very good performance reasons, use the same session for all your flows: opening a session for every flow like this is very expensive. What’s more, you’re then disposing of the context - again, this is uncessary.  
This is speculation, but what might be happening is some kind of race condition between consumer1 being closed, consumer 2 starting, then the underlying session or context being disposed.  
I recommend trying the 1 session, 1 context approach, and seeing if that fixes your problem.

---

<div class="post-metadata">

**Author:** ![Jordi](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordi](https://community.solace.com/u/Jordi)\
**Post date:** [June 23, 2022, 7:08am UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/10 "2022-06-23T07:08:03Z")

</div>

Hi TomF,  
yes, some sort of race condition is our suspition too,  
Thanks for your hint not to open one session per flow. Usually, this is fine for us, as we do not open several sessions one after the other, except in this situation, when we run multiple test. I will change the code in tht respect.  
Best,  
Jordi

---

<div class="post-metadata">

**Author:** ![TomF](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/tomf/32/1749_2.png) [@TomF](https://community.solace.com/u/TomF)\
**Post date:** [June 24, 2022, 8:15am UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/11 "2022-06-24T08:15:55Z")

</div>

Hi @Jordi ,  
Thanks for keeping us up to date. If that suggestion did fix your problem, please let us know!

---

<div class="post-metadata">

**Author:** ![Jordi](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jordi](https://community.solace.com/u/Jordi)\
**Post date:** [June 24, 2022, 11:02am UTC](https://community.solace.com/t/why-is-the-flow-in-the-handlemessageevent-sometimes-null/1288/12 "2022-06-24T11:02:05Z")

</div>

Just a short follow up: indeed, when we just open one session and multipe flows. the problem of flows becoming null is gone. Thanks again for this fruitful discussion.
