Skip to content

Commit 5499109

Browse files
committed
Fix: Worker locked in pending destruction state but not destroyed
1 parent 49d45fa commit 5499109

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

PowerThreadPool/Worker.cs

+30-34
Original file line numberDiff line numberDiff line change
@@ -320,26 +320,29 @@ private void AssignWork()
320320
Interlocked.Decrement(ref waitingWorkCount);
321321
}
322322
}
323+
}
323324

324-
if (waitingWorkID == null || work == null)
325-
{
326-
Interlocked.Exchange(ref workerState, 0);
325+
if (waitingWorkID == null || work == null)
326+
{
327+
Interlocked.Exchange(ref workerState, 0);
327328

328-
Interlocked.Decrement(ref powerPool.runningWorkerCount);
329-
PowerPoolOption powerPoolOption = powerPool.PowerPoolOption;
329+
Interlocked.Decrement(ref powerPool.runningWorkerCount);
330+
PowerPoolOption powerPoolOption = powerPool.PowerPoolOption;
330331

331-
powerPool.idleWorkerDic[this.ID] = this;
332-
powerPool.idleWorkerQueue.Enqueue(this.ID);
332+
powerPool.idleWorkerDic[this.ID] = this;
333+
powerPool.idleWorkerQueue.Enqueue(this.ID);
333334

334-
powerPool.CheckPoolIdle();
335+
powerPool.CheckPoolIdle();
335336

336-
if (powerPoolOption.DestroyThreadOption != null && powerPool.IdleWorkerCount > powerPoolOption.DestroyThreadOption.MinThreads)
337+
if (powerPoolOption.DestroyThreadOption != null && powerPool.IdleWorkerCount > powerPoolOption.DestroyThreadOption.MinThreads)
338+
{
339+
this.killTimer = new System.Timers.Timer(powerPoolOption.DestroyThreadOption.KeepAliveTime);
340+
try
337341
{
338-
this.killTimer = new System.Timers.Timer(powerPoolOption.DestroyThreadOption.KeepAliveTime);
339-
try
342+
killTimer.AutoReset = false;
343+
killTimer.Elapsed += (s, e) =>
340344
{
341-
killTimer.AutoReset = false;
342-
killTimer.Elapsed += (s, e) =>
345+
if (powerPool.IdleWorkerCount > powerPoolOption.DestroyThreadOption.MinThreads)
343346
{
344347
SpinWait.SpinUntil(() =>
345348
{
@@ -349,33 +352,26 @@ private void AssignWork()
349352
int originalState = Interlocked.CompareExchange(ref workerState, 2, 0);
350353
if (originalState == 0)
351354
{
352-
if (powerPool.IdleWorkerCount > powerPoolOption.DestroyThreadOption.MinThreads && powerPool.idleWorkerDic.TryRemove(ID, out _))
355+
powerPool.idleWorkerDic.TryRemove(ID, out _);
356+
if (powerPool.aliveWorkerDic.TryRemove(ID, out _))
353357
{
354-
if (powerPool.aliveWorkerDic.TryRemove(ID, out _))
355-
{
356-
Interlocked.Decrement(ref powerPool.aliveWorkerCount);
357-
powerPool.aliveWorkerList = powerPool.aliveWorkerDic.Values;
358-
}
359-
Kill();
360-
361-
killTimer.Enabled = false;
358+
Interlocked.Decrement(ref powerPool.aliveWorkerCount);
359+
powerPool.aliveWorkerList = powerPool.aliveWorkerDic.Values;
362360
}
361+
Kill();
363362
}
364-
else
365-
{
366-
killTimer.Enabled = false;
367-
}
368-
};
363+
}
364+
killTimer.Enabled = false;
365+
};
369366

370-
killTimer.Start();
371-
}
372-
catch
373-
{
374-
}
367+
killTimer.Start();
368+
}
369+
catch
370+
{
375371
}
376-
377-
return;
378372
}
373+
374+
return;
379375
}
380376
}
381377

UnitTest/ControlTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -854,10 +854,10 @@ public async Task TestWaitByIDInterruptEnd()
854854
Thread.Sleep(10);
855855
}
856856
});
857-
Thread.Sleep(10);
857+
Thread.Sleep(100);
858858
Task<bool> task = powerPool.WaitAsync(id);
859859

860-
Thread.Sleep(100);
860+
Thread.Sleep(500);
861861
powerPool.Stop(true);
862862

863863
bool res = await task;

0 commit comments

Comments
 (0)