diff --git a/lib/tunnelbroker/tunnelbroker-context.js b/lib/tunnelbroker/tunnelbroker-context.js --- a/lib/tunnelbroker/tunnelbroker-context.js +++ b/lib/tunnelbroker/tunnelbroker-context.js @@ -261,7 +261,6 @@ type: tunnelbrokerMessageTypes.MESSAGE_RECEIVE_CONFIRMATION, messageIDs: [message.messageID], }; - socket.current?.send(JSON.stringify(confirmation)); let rawPeerToPeerMessage; try { @@ -271,11 +270,17 @@ 'error while parsing Tunnelbroker peer-to-peer message:', e.message, ); + // Client received incorrect message, confirm to remove from + // Tunnelbroker queue. + socket.current?.send(JSON.stringify(confirmation)); return; } if (!peerToPeerMessageValidator.is(rawPeerToPeerMessage)) { console.log('invalid Tunnelbroker PeerToPeerMessage'); + // The client received an invalid Tunnelbroker message, + // and cannot process this type of request. + socket.current?.send(JSON.stringify(confirmation)); return; } const peerToPeerMessage: PeerToPeerMessage = rawPeerToPeerMessage; @@ -301,6 +306,14 @@ ); } catch (e) { console.log(e.message); + } finally { + if ( + localSocketSessionCounter === socketSessionCounter.current && + socket.current + ) { + // We confirm regardless of success or error while processing. + socket.current.send(JSON.stringify(confirmation)); + } } })(); } else if (