Skip to content

Commit f14844a

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 f14844a

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2020,7 +2020,7 @@ private boolean isFatalError(ErrorInfo err) {
20202020
private ErrorInfo stateError;
20212021
private ConnectParams pendingConnect;
20222022
private boolean suppressRetry; /* for tests only; modified via reflection */
2023-
private ITransport transport;
2023+
private volatile ITransport transport;
20242024
private long suspendTime;
20252025
public long msgSerial;
20262026
private long lastActivity;

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)