Skip to content

Commit

Permalink
Merge pull request #94 from jhonabreul/bug-weekly-restart-race-condition
Browse files Browse the repository at this point in the history
Fix race condition in manual weekly restart
  • Loading branch information
jhonabreul authored Feb 1, 2024
2 parents bffd005 + 92914b9 commit 027d4f9
Showing 1 changed file with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,10 @@ public override List<CashAmount> GetCashBalance()
/// </summary>
public override void Connect()
{
if (IsConnected) return;
if (IsConnected || _isDisposeCalled)
{
return;
}

_stateManager.IsConnecting = true;

Expand All @@ -777,7 +780,8 @@ public override void Connect()
Log.Trace($"InteractiveBrokersBrokerage.Connect(): Data subscription count {subscribedSymbolsCount}, restoring data subscriptions is required");
}

while (true)
// While not disposed instead of while(true). This could be happening in a different thread than the dispose call, so let's be safe.
while (!_isDisposeCalled)
{
try
{
Expand Down Expand Up @@ -952,11 +956,13 @@ private bool HeartBeat(int waitTimeMs)
return true;
}

if (!_ibAutomater.IsWithinScheduledServerResetTimes() && IsConnected
if (!_isDisposeCalled &&
!_ibAutomater.IsWithinScheduledServerResetTimes() &&
IsConnected &&
// do not run heart beat if we are close to daily restarts
&& DateTime.Now.TimeOfDay < _heartBeatTimeLimit
DateTime.Now.TimeOfDay < _heartBeatTimeLimit &&
// do not run heart beat if we are restarting
&& !IsRestartInProgress())
!IsRestartInProgress())
{
_currentTimeEvent.Reset();
// request current time to the server
Expand Down Expand Up @@ -4577,13 +4583,27 @@ private void StartGatewayWeeklyRestartTask()
return;
}

var restart = false;

lock (_lastIBAutomaterExitTimeLock)
{
// if the gateway hasn't yet exited today, we restart manually
if (_lastIBAutomaterExitTime.Date < DateTime.UtcNow.Date)
{
restart = true;
}
else
{
Log.Trace($"InteractiveBrokersBrokerage.StartGatewayWeeklyRestartTask(): skip restart: gateway already exited today and should have been automatically restarted.");
}
}

if (restart)
{
Log.Trace($"InteractiveBrokersBrokerage.StartGatewayWeeklyRestartTask(): triggering weekly restart manually");

try
{
if (_ibAutomater.IsRunning())
{
// stopping the gateway will make the IBAutomater emit the exit event, which will trigger the restart
Expand All @@ -4595,9 +4615,9 @@ private void StartGatewayWeeklyRestartTask()
CheckIbAutomaterError(_ibAutomater.Start(false));
}
}
else
catch (Exception ex)
{
Log.Trace($"InteractiveBrokersBrokerage.StartGatewayWeeklyRestartTask(): skip restart: gateway already exited today and should have been automatically restarted.");
Log.Error(ex);
}
}

Expand Down

0 comments on commit 027d4f9

Please sign in to comment.