<div dir="ltr">Thanks for the clarification. I think I&#39;m OK, but want to double check.<div><br></div><div>I have some code that will pass an exchange object between thread pools not owned by Undertow. I can guarantee that I won&#39;t interact with the exchange from more than one thread at at time, but I will move it from thread pool to thread pool. For example, I might get a request and call dispatch. I&#39;d then call a function that extracts a header from the exchange and then does an async lookup in a database for the info that was found in the header. When the DB lookup is complete I&#39;ll get a callback in a thread owned by the DB. In that callback I might extract another header from the exchange and then do another async lookup in a different database with a different thread pool. When that call is complete I&#39;ll get a callback in yet another thread, again not owned by Undertow. In that final callback I&#39;d call &quot;exchange.getResponseSender().send(...)&quot; to send the final response.</div><div><br></div><div>If I guarantee that I&#39;m not operating on the exchange in more than one thread at a time I shouldn&#39;t have to worry about synchronization. However, since some async HttpServerExchange operations, like receiveFullBytes, can put me back on the Undertow IO pool I do need to check &quot;exchange.isInIoThread()&quot; and, if that returns true, I then need to call dispatch again. </div><div><br></div><div>Does that all sound correct?</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Nov 22, 2016 at 1:12 PM Stuart Douglas &lt;<a href="mailto:sdouglas@redhat.com">sdouglas@redhat.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Wed, Nov 23, 2016 at 7:56 AM, Oliver Dain &lt;<a href="mailto:oliver@analyticspot.com" class="gmail_msg" target="_blank">oliver@analyticspot.com</a>&gt; wrote:<br class="gmail_msg">
&gt; Wow! Thanks for the super-fast reply.<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; I think I&#39;m a little confused about the difference between being in an IO<br class="gmail_msg">
&gt; thread and being dispatched. Is it the case that if the server thinks I&#39;m<br class="gmail_msg">
&gt; &quot;not dispatched&quot; (that is, if the server will send a response on my behalf<br class="gmail_msg">
&gt; when my method returns) that I&#39;m always on an IO thread? I often do some<br class="gmail_msg">
&gt; work on my own thread pools and those threads often interact with the<br class="gmail_msg">
&gt; HttpServerExchange object. I know that some interactions with the exchange<br class="gmail_msg">
&gt; object (e.g. recieveFullBytes) will take an exchange that is in a dispatched<br class="gmail_msg">
&gt; state and put it in a non-dispatched state. So I want to just keep checking<br class="gmail_msg">
&gt; that I&#39;m dispatched and, if I&#39;m not, dispatch again. So my code might look<br class="gmail_msg">
&gt; something like:<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; void handle(HttpServerExchange exchange) {<br class="gmail_msg">
&gt;     exchange.dispatch(myOwnThreadPoolExecutor, () -&gt; {<br class="gmail_msg">
&gt;         <a href="http://log.info" rel="noreferrer" class="gmail_msg" target="_blank">log.info</a>(&quot;I am not not on an IO thread and I am dispatched.&quot;);<br class="gmail_msg">
&gt;         doSomethingWithExchange(exchange);<br class="gmail_msg">
&gt;         assert exchange.isDispatched();<br class="gmail_msg">
&gt;         assert !exchange.isInIoThread();<br class="gmail_msg">
&gt;         // how do I check to see if I&#39;m still in a dispatched state?<br class="gmail_msg">
&gt;     });<br class="gmail_msg">
&gt; }<br class="gmail_msg">
<br class="gmail_msg">
So the basic idea behind the &#39;dispatch&#39; is that it is a way of<br class="gmail_msg">
guaranteeing thread safety without needing to synchronize the exchange<br class="gmail_msg">
object. Effectively it allows you to pass an exchange from one thread<br class="gmail_msg">
to another in a way that guarantees that the first thread is &#39;done&#39;<br class="gmail_msg">
with the exchange before the second thread starts.<br class="gmail_msg">
<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; (note: the above is a bad, super-simplified example and in the actual code<br class="gmail_msg">
&gt; there&#39;s a lot of asynchronous stuff happening that bounces the execution<br class="gmail_msg">
&gt; from one thread pool to another).<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; The reason for all of this is that I have some edge cases which appear to be<br class="gmail_msg">
&gt; race conditions in which I know for sure that I call exchange.dispatch() and<br class="gmail_msg">
&gt; then I start doing some work but the exchange gets ended before I&#39;ve<br class="gmail_msg">
&gt; finished my computations and have a response to send leaving the client with<br class="gmail_msg">
&gt; an empty response. I&#39;m not entirely sure how that could happen, but my best<br class="gmail_msg">
&gt; guess right now is that I interact with the exchange in some way that causes<br class="gmail_msg">
&gt; it to no longer be in in the dispatched state and, as a result, the<br class="gmail_msg">
&gt; HttpServerExchange is ending the exchange for me before I&#39;m ready.<br class="gmail_msg">
<br class="gmail_msg">
I think I would need to look at a more specific example of what you<br class="gmail_msg">
are doing, but it is possibly related to how you are handing stuff in<br class="gmail_msg">
other threads? The exchange itself is not thread safe, if you start<br class="gmail_msg">
doing work on the exchange in another thread without having dispatched<br class="gmail_msg">
you can get a race condition.<br class="gmail_msg">
<br class="gmail_msg">
Stuart<br class="gmail_msg">
<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; Thanks again,<br class="gmail_msg">
&gt; Oliver<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; On Tue, Nov 22, 2016 at 12:47 PM Stuart Douglas &lt;<a href="mailto:sdouglas@redhat.com" class="gmail_msg" target="_blank">sdouglas@redhat.com</a>&gt; wrote:<br class="gmail_msg">
&gt;&gt;<br class="gmail_msg">
&gt;&gt; After the call stack returns and the dispatch actually happens<br class="gmail_msg">
&gt;&gt; isDispatched() will return false (allowing the exchange to be<br class="gmail_msg">
&gt;&gt; dispatched again).<br class="gmail_msg">
&gt;&gt;<br class="gmail_msg">
&gt;&gt; In general you should not really need to check this anyway, if you<br class="gmail_msg">
&gt;&gt; want to know if you are in the IO thread you should use isInIiThread()<br class="gmail_msg">
&gt;&gt; instead.<br class="gmail_msg">
&gt;&gt;<br class="gmail_msg">
&gt;&gt; Stuart<br class="gmail_msg">
&gt;&gt;<br class="gmail_msg">
&gt;&gt; On Wed, Nov 23, 2016 at 7:44 AM, Oliver Dain &lt;<a href="mailto:oliver@analyticspot.com" class="gmail_msg" target="_blank">oliver@analyticspot.com</a>&gt;<br class="gmail_msg">
&gt;&gt; wrote:<br class="gmail_msg">
&gt;&gt; &gt; I&#39;ve got some pretty simple code that looks something like:<br class="gmail_msg">
&gt;&gt; &gt;<br class="gmail_msg">
&gt;&gt; &gt; if (!exhange.isDispatched()) {<br class="gmail_msg">
&gt;&gt; &gt;     exchange.dispatch(() -&gt; {<br class="gmail_msg">
&gt;&gt; &gt;         assert exchange.isDispatched();<br class="gmail_msg">
&gt;&gt; &gt;          // more stuff....<br class="gmail_msg">
&gt;&gt; &gt;     });<br class="gmail_msg">
&gt;&gt; &gt; }<br class="gmail_msg">
&gt;&gt; &gt;<br class="gmail_msg">
&gt;&gt; &gt; but the assert fails. That is, when dispatch calls my runnable<br class="gmail_msg">
&gt;&gt; &gt; isDispatched<br class="gmail_msg">
&gt;&gt; &gt; is not true.<br class="gmail_msg">
&gt;&gt; &gt;<br class="gmail_msg">
&gt;&gt; &gt; I believe I have a bug somewhere with how/when dispatches are happening<br class="gmail_msg">
&gt;&gt; &gt; so<br class="gmail_msg">
&gt;&gt; &gt; I&#39;ve littered my code with such asserts and they fail reliably even when<br class="gmail_msg">
&gt;&gt; &gt; I&#39;m<br class="gmail_msg">
&gt;&gt; &gt; quite sure the exchange has been dispatched (e.g. when it is literally<br class="gmail_msg">
&gt;&gt; &gt; the<br class="gmail_msg">
&gt;&gt; &gt; first call in a Runnable passed to to the dispatch() method).<br class="gmail_msg">
&gt;&gt; &gt;<br class="gmail_msg">
&gt;&gt; &gt; What am I doing wrong?<br class="gmail_msg">
&gt;&gt; &gt; --<br class="gmail_msg">
&gt;&gt; &gt; CTO, Analytic Spot<br class="gmail_msg">
&gt;&gt; &gt; 44 West Broadway #222<br class="gmail_msg">
&gt;&gt; &gt; Eugene, OR 97401<br class="gmail_msg">
&gt;&gt; &gt; <a href="http://analyticspot.com" rel="noreferrer" class="gmail_msg" target="_blank">analyticspot.com</a> • <a href="tel:(425)%20296-6556" value="+14252966556" class="gmail_msg" target="_blank">425-296-6556</a><br class="gmail_msg">
&gt;&gt; &gt; <a href="http://www.linkedin.com/in/oliverdain" rel="noreferrer" class="gmail_msg" target="_blank">www.linkedin.com/in/oliverdain</a><br class="gmail_msg">
&gt;&gt; &gt;<br class="gmail_msg">
&gt;&gt; &gt; _______________________________________________<br class="gmail_msg">
&gt;&gt; &gt; undertow-dev mailing list<br class="gmail_msg">
&gt;&gt; &gt; <a href="mailto:undertow-dev@lists.jboss.org" class="gmail_msg" target="_blank">undertow-dev@lists.jboss.org</a><br class="gmail_msg">
&gt;&gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.jboss.org/mailman/listinfo/undertow-dev</a><br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; --<br class="gmail_msg">
&gt; CTO, Analytic Spot<br class="gmail_msg">
&gt; 44 West Broadway #222<br class="gmail_msg">
&gt; Eugene, OR 97401<br class="gmail_msg">
&gt; <a href="http://analyticspot.com" rel="noreferrer" class="gmail_msg" target="_blank">analyticspot.com</a> • <a href="tel:(425)%20296-6556" value="+14252966556" class="gmail_msg" target="_blank">425-296-6556</a><br class="gmail_msg">
&gt; <a href="http://www.linkedin.com/in/oliverdain" rel="noreferrer" class="gmail_msg" target="_blank">www.linkedin.com/in/oliverdain</a><br class="gmail_msg">
</blockquote></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature"><div dir="ltr"><div style="font-size:small"><div style="font-size:13px;line-height:19.5px">CTO, Analytic Spot</div><div style="font-size:13px;line-height:19.5px">44 West Broadway #222</div><div style="font-size:13px;line-height:19.5px">Eugene, OR 97401<br></div><div style="font-size:13px;line-height:19.5px"><a href="http://analyticspot.com/" style="z-index: 0;">analyticspot.com</a> <span style="color:rgb(127,127,127);font-family:&#39;helvetica neue&#39;;font-size:11px;line-height:normal">• </span>425-296-6556</div></div><div style="font-size:small"><span style="line-height:19.5px"><a href="http://www.linkedin.com/in/oliverdain" style="z-index: 0;">www.linkedin.com/in/oliverdain</a></span></div></div></div>