Skip to content

Commit

Permalink
Fix bugs in idle timeout logic (C#) (#3156)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Nov 16, 2024
1 parent 8911c92 commit 017cbd9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
3 changes: 3 additions & 0 deletions csharp/src/Ice/ConnectionI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,9 @@ private bool validate(int operation)
throw new MarshalException($"Received ValidateConnection message with unexpected size {size}.");
}
TraceUtil.traceRecv(_readStream, this, _logger, _traceLevels);

// Client connection starts sending heartbeats once it's received the ValidateConnection message.
_idleTimeoutTransceiver?.scheduleHeartbeat();
}
}

Expand Down
29 changes: 14 additions & 15 deletions csharp/src/Ice/Internal/IdleTimeoutTransceiverDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#nullable enable

using System.Collections;
using System.Diagnostics;
using System.Net.Sockets;

Expand Down Expand Up @@ -61,17 +60,8 @@ public void finishRead(Buffer buf)

public ConnectionInfo getInfo() => _decoratee.getInfo();

public int initialize(Buffer readBuffer, Buffer writeBuffer, ref bool hasMoreData)
{
int op = _decoratee.initialize(readBuffer, writeBuffer, ref hasMoreData);

if (op == SocketOperation.None) // connected
{
rescheduleWriteTimer();
}

return op;
}
public int initialize(Buffer readBuffer, Buffer writeBuffer, ref bool hasMoreData) =>
_decoratee.initialize(readBuffer, writeBuffer, ref hasMoreData);

public string protocol() => _decoratee.protocol();

Expand Down Expand Up @@ -108,7 +98,11 @@ public int write(Buffer buf)
return op;
}

internal IdleTimeoutTransceiverDecorator(Transceiver decoratee, ConnectionI connection, TimeSpan idleTimeout, bool enableIdleCheck)
internal IdleTimeoutTransceiverDecorator(
Transceiver decoratee,
ConnectionI connection,
TimeSpan idleTimeout,
bool enableIdleCheck)
{
Debug.Assert(idleTimeout > TimeSpan.Zero);

Expand All @@ -118,9 +112,12 @@ internal IdleTimeoutTransceiverDecorator(Transceiver decoratee, ConnectionI conn
if (enableIdleCheck)
{
_readTimer = new System.Threading.Timer(_ => connection.idleCheck(_idleTimeout));
}
idleCheckEnabled = enableIdleCheck;

// idleCheckEnabled remains false for now. ConnectionI calls enableIdleCheck() when it becomes Active to
// schedule the first idle check and set idleCheckEnabled to true.
// We don't want the idle check to start when a client connection is waiting for the initial
// ValidateConnection message or when a server connection is in its initial StateHolding state.
}
_writeTimer = new System.Threading.Timer(_ => connection.sendHeartbeat());
}

Expand All @@ -142,6 +139,8 @@ internal void disableIdleCheck()
}
}

internal void scheduleHeartbeat() => rescheduleWriteTimer();

private void cancelReadTimer() => _readTimer!.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);

private void cancelWriteTimer() => _writeTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
Expand Down

0 comments on commit 017cbd9

Please sign in to comment.