@@ -64,7 +64,7 @@ public final class OpenCliWsReverseAgentClient implements AutoCloseable {
6464
6565 private volatile boolean shutdownRequested ;
6666
67- private volatile WebSocketClient activeClient ;
67+ private final AtomicReference < WebSocketClient > activeClient = new AtomicReference <>() ;
6868
6969 private Thread runnerThread ;
7070
@@ -112,8 +112,7 @@ public synchronized void start() {
112112 */
113113 public synchronized void stop () {
114114 shutdownRequested = true ;
115- WebSocketClient client = activeClient ;
116- activeClient = null ;
115+ WebSocketClient client = activeClient .getAndSet (null );
117116 if (Objects .nonNull (client )) {
118117 try {
119118 client .close ();
@@ -200,6 +199,10 @@ private void runOneSession() throws Exception {
200199 new WebSocketClient (endpoint ) {
201200 @ Override
202201 public void onOpen (ServerHandshake handshake ) {
202+ if (!isCurrentSession (this )) {
203+ closeSession (this );
204+ return ;
205+ }
203206 try {
204207 send (buildRegisterJson ());
205208 } catch (Exception e ) {
@@ -220,6 +223,7 @@ public void onMessage(String message) {
220223
221224 @ Override
222225 public void onClose (int code , String reason , boolean remote ) {
226+ activeClient .compareAndSet (this , null );
223227 if (registered .getCount () > 0 ) {
224228 handshakeFailure .compareAndSet (
225229 null ,
@@ -234,6 +238,7 @@ public void onClose(int code, String reason, boolean remote) {
234238
235239 @ Override
236240 public void onError (Exception ex ) {
241+ activeClient .compareAndSet (this , null );
237242 log .warn ("WebSocket error" );
238243 if (registered .getCount () > 0 ) {
239244 handshakeFailure .compareAndSet (null , ex );
@@ -243,26 +248,48 @@ public void onError(Exception ex) {
243248 }
244249 };
245250
246- activeClient = client ;
247- if (!client .connectBlocking (30 , TimeUnit .SECONDS )) {
248- throw new IllegalStateException ("WebSocket connectBlocking returned false" );
251+ if (!publishSession (client )) {
252+ return ;
249253 }
254+ try {
255+ if (!client .connectBlocking (30 , TimeUnit .SECONDS )) {
256+ throw new IllegalStateException ("WebSocket connectBlocking returned false" );
257+ }
258+ if (!registered .await (connectionProperties .getHandshakeTimeoutMillis (), TimeUnit .MILLISECONDS )) {
259+ throw new IllegalStateException ("Timeout waiting for 'registered' from center" );
260+ }
261+ Throwable hf = handshakeFailure .get ();
262+ if (Objects .nonNull (hf )) {
263+ if (hf instanceof Exception ) {
264+ throw (Exception ) hf ;
265+ }
266+ throw new IllegalStateException (hf );
267+ }
268+ sessionClosed .await ();
269+ } finally {
270+ activeClient .compareAndSet (client , null );
271+ closeSession (client );
272+ }
273+ }
250274
251- if (! registered . await ( connectionProperties . getHandshakeTimeoutMillis (), TimeUnit . MILLISECONDS ) ) {
252- client . close ();
253- throw new IllegalStateException ( "Timeout waiting for 'registered' from center" ) ;
275+ private synchronized boolean publishSession ( WebSocketClient client ) {
276+ if ( shutdownRequested ) {
277+ return false ;
254278 }
255- Throwable hf = handshakeFailure .get ();
256- if (Objects .nonNull (hf )) {
279+ activeClient .set (client );
280+ return true ;
281+ }
282+
283+ private boolean isCurrentSession (WebSocketClient client ) {
284+ return !shutdownRequested && activeClient .get () == client ;
285+ }
286+
287+ private static void closeSession (WebSocketClient client ) {
288+ try {
257289 client .close ();
258- if (hf instanceof Exception ) {
259- throw (Exception ) hf ;
260- }
261- throw new IllegalStateException (hf );
290+ } catch (Exception e ) {
291+ log .debug ("ws session close failed" );
262292 }
263-
264- sessionClosed .await ();
265- activeClient = null ;
266293 }
267294
268295 private void validateConnectionProperties () {
@@ -315,6 +342,9 @@ private void handleTextMessage(
315342 CountDownLatch registered ,
316343 AtomicReference <Throwable > handshakeFailure )
317344 throws Exception {
345+ if (!isCurrentSession (ws )) {
346+ return ;
347+ }
318348 JsonNode root = MAPPER .readTree (payload );
319349 String type = root .path ("type" ).asText ("" );
320350 if ("registered" .equals (type )) {
@@ -335,7 +365,7 @@ private void handleTextMessage(
335365 sendResult (ws , result ("" , false , Collections .emptyList (), "invalid worker request ID" ));
336366 return ;
337367 }
338- if (shutdownRequested || !ws .isOpen () || registered .getCount () > 0
368+ if (! isCurrentSession ( ws ) || !ws .isOpen () || registered .getCount () > 0
339369 || Objects .nonNull (handshakeFailure .get ())) {
340370 sendResult (ws , result (requestId , false , Collections .emptyList (), "worker is not registered" ));
341371 return ;
@@ -378,7 +408,7 @@ private void runCollectSafe(WebSocketClient ws, OpenCliWsCollectRequests.Key key
378408 } finally {
379409 collectRequests .complete (key , response );
380410 }
381- if (Objects .nonNull (response ) && ws .isOpen ()) {
411+ if (Objects .nonNull (response ) && isCurrentSession ( ws ) && ws .isOpen ()) {
382412 sendResult (ws , response );
383413 }
384414 }
0 commit comments