From b9de5c85635741d08ea636337b653af6c53abf3c Mon Sep 17 00:00:00 2001
From: Pieter Hintjens <ph@imatix.com>
Date: Fri, 20 May 2011 17:37:21 +0200
Subject: [PATCH] Modified mailbox:245 to deal better with socket corruption

Signed-off-by: Pieter Hintjens <ph@imatix.com>
---
 src/mailbox.cpp |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/mailbox.cpp b/src/mailbox.cpp
index 221396b..d25f342 100644
--- a/src/mailbox.cpp
+++ b/src/mailbox.cpp
@@ -219,7 +219,7 @@ int zmq::mailbox_t::recv (command_t *cmd_, bool block_)
     //  it to caller.
     int err = 0;
     ssize_t nbytes = ::recv (r, cmd_, sizeof (command_t), 0);
-    if (nbytes == -1 && (errno == EAGAIN || errno == EINTR))  
+    if (nbytes == -1 && (errno == EAGAIN || errno == EINTR))
         err = errno;
 
     //  Re-set the reader to non-blocking mode.
@@ -242,8 +242,11 @@ int zmq::mailbox_t::recv (command_t *cmd_, bool block_)
     errno_assert (nbytes != -1);
 
     //  Check whether we haven't got half of command.
-    zmq_assert (nbytes == sizeof (command_t));
-
+    if (nbytes != sizeof (command_t)) {
+        fprintf (stderr, "E: Likely attempt to use a socket from multiple threads\n");
+        fprintf (stderr, "I: Sockets are not threadsafe. Exiting application now.\n");
+        zmq_assert (0);
+    }
     return 0;
 }
 
-- 
1.7.1

