Skip to content

Commit 6315c4b

Browse files
committed
fix: move simTime into lock, local capture _inner to prevent mid-swap reads
1 parent 0533a57 commit 6315c4b

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

src/ResQ.Viz.Web/Services/SimulationService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,16 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
168168
{
169169
while (!stoppingToken.IsCancellationRequested)
170170
{
171+
bool shouldBroadcast;
171172
lock (_lock)
172173
{
173174
_world.Step();
174175
_tickCount++;
176+
_simTime += 1.0 / 60.0;
177+
shouldBroadcast = _tickCount % 6 == 0;
175178
}
176179

177-
if (_tickCount % 6 == 0)
180+
if (shouldBroadcast)
178181
{
179182
FrameReady?.Invoke(this, EventArgs.Empty);
180183

@@ -183,8 +186,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
183186
var frame = _frameBuilder.Build(snapshot, _simTime);
184187
await _hubContext.Clients.All.SendAsync("ReceiveFrame", frame, stoppingToken);
185188
}
186-
187-
_simTime += 1.0 / 60.0;
188189
await Task.Delay(16, stoppingToken);
189190
}
190191
}

src/ResQ.Viz.Web/Services/UpdatableWeatherSystem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public void Update(WeatherConfig config)
4343
=> _inner = new WeatherSystem(config);
4444

4545
/// <inheritdoc/>
46-
public double Visibility => _inner.Visibility;
46+
public double Visibility { get { var inner = _inner; return inner.Visibility; } }
4747

4848
/// <inheritdoc/>
49-
public double Precipitation => _inner.Precipitation;
49+
public double Precipitation { get { var inner = _inner; return inner.Precipitation; } }
5050

5151
/// <inheritdoc/>
52-
public Vector3 GetWind(double x, double y, double z) => _inner.GetWind(x, y, z);
52+
public Vector3 GetWind(double x, double y, double z) { var inner = _inner; return inner.GetWind(x, y, z); }
5353

5454
/// <inheritdoc/>
55-
public void Step(double dt) => _inner.Step(dt);
55+
public void Step(double dt) { var inner = _inner; inner.Step(dt); }
5656
}

0 commit comments

Comments
 (0)