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