Merge branch 'master' of git://git.evergreen-ils.org/OpenSRF
authorEquinox <info@esilibrary.com>
Thu, 1 Feb 2018 20:54:01 +0000 (15:54 -0500)
committerEquinox <info@esilibrary.com>
Thu, 1 Feb 2018 20:54:01 +0000 (15:54 -0500)
src/gateway/osrf_websocket_translator.c

index ef8d4af..2d3a5e1 100644 (file)
@@ -344,10 +344,26 @@ void* APR_THREAD_FUNC osrf_responder_thread_main(apr_thread_t *thread, void *dat
             return NULL;
         }
 
-        // wait for a response
+        // wait indefinitely for a response
         tmsg = client_recv(osrf_handle, -1);
 
-        if (!tmsg) continue; // interrupt
+        if (!tmsg) {
+            // tmsg can only be NULL if the underlying select() call is
+            // interrupted or the jabber socket connection was severed.
+
+            if (client_connected(osrf_handle) &&
+                socket_connected(osrf_handle->session->sock_id)) {
+                continue; // interrupted.  restart loop.
+            }
+
+            // Socket connection was broken.  Send disconnect to client,
+            // causing on_disconnect_handler to run and cleanup.
+            osrfLogWarning(OSRF_LOG_MARK, 
+                "WS: Jabber socket disconnected. Sending close() to client");
+
+            trans->server->close(trans->server);
+            return NULL; // exit thread
+        }
 
         if (trans->client_connected) {
 
@@ -895,8 +911,12 @@ static size_t on_message_handler_body(void *data,
         msg_body, NULL, thread, recipient, NULL);
 
     message_set_osrf_xid(tmsg, osrfLogGetXid());
-    client_send_message(osrf_handle, tmsg);
 
+    size_t stat = OK;
+    if (client_send_message(osrf_handle, tmsg) != 0) {
+        osrfLogError(OSRF_LOG_MARK, "WS failed sending data to OpenSRF");
+        stat = HTTP_INTERNAL_SERVER_ERROR;
+    }
 
     osrfLogClearXid();
     message_free(tmsg);                                                         
@@ -904,7 +924,7 @@ static size_t on_message_handler_body(void *data,
     free(msg_body);
 
     last_activity_time = time(NULL);
-    return OK;
+    return stat;
 }
 
 static size_t CALLBACK on_message_handler(void *data,
@@ -913,16 +933,25 @@ static size_t CALLBACK on_message_handler(void *data,
 
     if (apr_thread_mutex_lock(trans->mutex) != APR_SUCCESS) {
         osrfLogError(OSRF_LOG_MARK, "WS error locking thread mutex");
-        return 1; // TODO: map to apr_status_t value?
+        return 1;
     }
 
-    apr_status_t stat = on_message_handler_body(data, server, type, buffer, buffer_size);
+    size_t stat = on_message_handler_body(data, server, type, buffer, buffer_size);
 
     if (apr_thread_mutex_unlock(trans->mutex) != APR_SUCCESS) {
         osrfLogError(OSRF_LOG_MARK, "WS error locking thread mutex");
         return 1;
     }
 
+    if (stat != OK) {
+        // Returning a non-OK status alone won't force a disconnect.
+        // Once disconnected, the on_disconnect_handler() handler
+        // will run, clean it all up, and kill the process.
+        osrfLogError(OSRF_LOG_MARK,
+            "Error relaying message, forcing client disconnect");
+        trans->server->close(trans->server);
+    }
+
     return stat;
 }