@@ -65,7 +65,7 @@ public final class OpenCliWsReverseAgentClient implements AutoCloseable {
6565
6666 private volatile boolean shutdownRequested ;
6767
68- private volatile WebSocketClient activeClient ;
68+ private final AtomicReference < WebSocketClient > activeClient = new AtomicReference <>() ;
6969
7070 private Thread runnerThread ;
7171
@@ -113,8 +113,7 @@ public synchronized void start() {
113113 */
114114 public synchronized void stop () {
115115 shutdownRequested = true ;
116- WebSocketClient client = activeClient ;
117- activeClient = null ;
116+ WebSocketClient client = activeClient .getAndSet (null );
118117 if (Objects .nonNull (client )) {
119118 try {
120119 client .close ();
@@ -201,6 +200,10 @@ private void runOneSession() throws Exception {
201200 new WebSocketClient (endpoint ) {
202201 @ Override
203202 public void onOpen (ServerHandshake handshake ) {
203+ if (!isCurrentSession (this )) {
204+ closeSession (this );
205+ return ;
206+ }
204207 try {
205208 send (buildRegisterJson ());
206209 } catch (Exception e ) {
@@ -221,6 +224,7 @@ public void onMessage(String message) {
221224
222225 @ Override
223226 public void onClose (int code , String reason , boolean remote ) {
227+ activeClient .compareAndSet (this , null );
224228 if (registered .getCount () > 0 ) {
225229 handshakeFailure .compareAndSet (
226230 null ,
@@ -235,6 +239,7 @@ public void onClose(int code, String reason, boolean remote) {
235239
236240 @ Override
237241 public void onError (Exception ex ) {
242+ activeClient .compareAndSet (this , null );
238243 log .warn ("WebSocket error" );
239244 if (registered .getCount () > 0 ) {
240245 handshakeFailure .compareAndSet (null , ex );
@@ -244,26 +249,48 @@ public void onError(Exception ex) {
244249 }
245250 };
246251
247- activeClient = client ;
248- if (!client .connectBlocking (30 , TimeUnit .SECONDS )) {
249- throw new IllegalStateException ("WebSocket connectBlocking returned false" );
252+ if (!publishSession (client )) {
253+ return ;
250254 }
255+ try {
256+ if (!client .connectBlocking (30 , TimeUnit .SECONDS )) {
257+ throw new IllegalStateException ("WebSocket connectBlocking returned false" );
258+ }
259+ if (!registered .await (connectionProperties .getHandshakeTimeoutMillis (), TimeUnit .MILLISECONDS )) {
260+ throw new IllegalStateException ("Timeout waiting for 'registered' from center" );
261+ }
262+ Throwable hf = handshakeFailure .get ();
263+ if (Objects .nonNull (hf )) {
264+ if (hf instanceof Exception ) {
265+ throw (Exception ) hf ;
266+ }
267+ throw new IllegalStateException (hf );
268+ }
269+ sessionClosed .await ();
270+ } finally {
271+ activeClient .compareAndSet (client , null );
272+ closeSession (client );
273+ }
274+ }
251275
252- if (! registered . await ( connectionProperties . getHandshakeTimeoutMillis (), TimeUnit . MILLISECONDS ) ) {
253- client . close ();
254- throw new IllegalStateException ( "Timeout waiting for 'registered' from center" ) ;
276+ private synchronized boolean publishSession ( WebSocketClient client ) {
277+ if ( shutdownRequested ) {
278+ return false ;
255279 }
256- Throwable hf = handshakeFailure .get ();
257- if (Objects .nonNull (hf )) {
280+ activeClient .set (client );
281+ return true ;
282+ }
283+
284+ private boolean isCurrentSession (WebSocketClient client ) {
285+ return !shutdownRequested && activeClient .get () == client ;
286+ }
287+
288+ private static void closeSession (WebSocketClient client ) {
289+ try {
258290 client .close ();
259- if (hf instanceof Exception ) {
260- throw (Exception ) hf ;
261- }
262- throw new IllegalStateException (hf );
291+ } catch (Exception e ) {
292+ log .debug ("ws session close failed" );
263293 }
264-
265- sessionClosed .await ();
266- activeClient = null ;
267294 }
268295
269296 private void validateConnectionProperties () {
@@ -316,6 +343,9 @@ private void handleTextMessage(
316343 CountDownLatch registered ,
317344 AtomicReference <Throwable > handshakeFailure )
318345 throws Exception {
346+ if (!isCurrentSession (ws )) {
347+ return ;
348+ }
319349 JsonNode root = MAPPER .readTree (payload );
320350 String type = root .path ("type" ).asText ("" );
321351 if ("registered" .equals (type )) {
@@ -336,7 +366,7 @@ private void handleTextMessage(
336366 sendResult (ws , result ("" , false , Collections .emptyList (), "invalid worker request ID" ));
337367 return ;
338368 }
339- if (shutdownRequested || !ws .isOpen () || registered .getCount () > 0
369+ if (! isCurrentSession ( ws ) || !ws .isOpen () || registered .getCount () > 0
340370 || Objects .nonNull (handshakeFailure .get ())) {
341371 sendResult (ws , result (requestId , false , Collections .emptyList (), "worker is not registered" ));
342372 return ;
@@ -379,7 +409,7 @@ private void runCollectSafe(WebSocketClient ws, OpenCliWsCollectRequests.Key key
379409 } finally {
380410 collectRequests .complete (key , response );
381411 }
382- if (Objects .nonNull (response ) && ws .isOpen ()) {
412+ if (Objects .nonNull (response ) && isCurrentSession ( ws ) && ws .isOpen ()) {
383413 sendResult (ws , response );
384414 }
385415 }
0 commit comments