Skip to content

Commit 718573c

Browse files
committed
chore: ensure thread-safe access to activity timer fields in WebSocketTransport
- marked `lastActivityTime` as volatile, it's low cost, but it will ensure that the timer reads fresh value, otherwise it can read very old value (although it's very unlikely) - check activity timer before going to the synchronized block
1 parent a6f0f75 commit 718573c

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/src/main/java/io/ably/lib/transport/WebSocketTransport.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ class WebSocketHandler implements WebSocketListener {
252252
***************************/
253253

254254
private final Timer timer = new Timer();
255-
private TimerTask activityTimerTask = null;
256-
private long lastActivityTime;
255+
private volatile TimerTask activityTimerTask = null;
256+
private volatile long lastActivityTime;
257257

258258
/**
259259
* Monitor for activity timer events
@@ -388,6 +388,9 @@ private void checkActivity() {
388388
return;
389389
}
390390

391+
// prevent going to the synchronized block if the timer is active
392+
if (activityTimerTask != null) return;
393+
391394
synchronized (activityTimerMonitor) {
392395
// Check if timer already running
393396
if (activityTimerTask == null) {

0 commit comments

Comments
 (0)