# InterprocessConnection teardown when connection is abruptly cut

**URL:** <https://forum.juce.com/t/interprocessconnection-teardown-when-connection-is-abruptly-cut/37349>\
**Category:** General JUCE discussion\
**Created:** [January 28, 2020, 5:10am UTC](https://forum.juce.com/t/interprocessconnection-teardown-when-connection-is-abruptly-cut/37349 "2020-01-28T05:10:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmelon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bmelon/32/2455_2.png) [@bmelon](https://forum.juce.com/u/bmelon)\
**Post date:** [January 28, 2020, 5:10am UTC](https://forum.juce.com/t/interprocessconnection-teardown-when-connection-is-abruptly-cut/37349/1 "2020-01-28T05:10:57Z")

</div>

I have a client/server with InterprocessConnection on both sides using a socket between them over a WAN. They use sendMessage and messageReceived like normal. If the connection between them is torn down abnormally, I hit a race condition. Let’s say one side has it’s ethernet cable removed abruptly. What I see is that one or both sides get stuck in sendMessage()-\>writeMessage() waiting for the outgoing data to get acknowledged by the other side. This locks pipeAndSocketLock. Meanwhile, I detect that the other side is gone at the application level because I have a ping going on. So, my application attempts to tear things down by calling disconnect(), but then gets stuck waiting on pipeAndSocketLock and things freeze up.

I would expect that disconnect() would call socket-\>close() unconditionally, and then maybe that would cause the blocked socket-\>send() call to return and error out (?). I could force this by accessing the getSocket()-\>close() to get around pipeAndSocketLock, but…I’m not confident about this. The internet seems to be saying that I need an asynchronous send(), which I doubt is planned for JUCE.

---

<div class="post-metadata">

**Author:** ![bmelon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bmelon/32/2455_2.png) [@bmelon](https://forum.juce.com/u/bmelon)\
**Post date:** [January 29, 2020, 12:44am UTC](https://forum.juce.com/t/interprocessconnection-teardown-when-connection-is-abruptly-cut/37349/2 "2020-01-29T00:44:45Z")

</div>

It appears that this works when I detect that the connection is dead. The detecting thread (Message thread in this case) can do this inside the InterprocessConnection class:

> ```
> if (getSocket()->getRawSocketHandle() >= 0)
> ::close(getSocket()->getRawSocketHandle());
> disconnect();
> 
> ```

The socket close will cause sendMessage to unblock in the send thread. The send thread looks like this:

> ```
> if (!sendMessage(data))
> {
> disconnect();
> }
> 
> ```

Might cause multiple calls to disconnect in this specific case, but seems safe and seems to work.
