Skip to content

Commit cdddf14

Browse files
fix(util): ensure JAction disposal if ExecuteAsync throws
Use 'using var' pattern consistently to ensure JAction is properly disposed even if ExecuteAsync() throws an exception. This addresses the code scanning alert about Dispose not being called on exception. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Signed-off-by: JasonXuDeveloper - 傑 <jason@xgamedev.net>
1 parent 0c191de commit cdddf14

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

UnityProject/Packages/com.jasonxudeveloper.jengine.util/Tests/Editor/JActionExecutionHandleTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,15 @@ public IEnumerator GetAwaiter_AfterCompletion_ReturnsCompletedAwaiter() => UniTa
240240
public IEnumerator Dispose_ReturnsActionToPool() => UniTask.ToCoroutine(async () =>
241241
{
242242
int initialCount = JAction.PooledCount;
243-
var action = JAction.Create().Do(() => { });
243+
// Use using to ensure action is disposed even if ExecuteAsync throws
244+
using var action = JAction.Create().Do(() => { });
244245
var handle = action.ExecuteAsync();
245246

246247
await handle;
247248
handle.Dispose();
248249

250+
// Note: action.Dispose() is called twice (by handle.Dispose and using),
251+
// but JAction.Dispose is idempotent so this is safe
249252
Assert.AreEqual(initialCount + 1, JAction.PooledCount);
250253
});
251254

@@ -260,7 +263,8 @@ public void Dispose_DefaultHandle_DoesNotThrow()
260263
[UnityTest]
261264
public IEnumerator Dispose_MultipleTimes_DoesNotThrow() => UniTask.ToCoroutine(async () =>
262265
{
263-
var action = JAction.Create().Do(() => { });
266+
// Use using to ensure action is disposed even if ExecuteAsync throws
267+
using var action = JAction.Create().Do(() => { });
264268
var handle = action.ExecuteAsync();
265269

266270
await handle;
@@ -279,15 +283,15 @@ public IEnumerator Dispose_MultipleTimes_DoesNotThrow() => UniTask.ToCoroutine(a
279283
[UnityTest]
280284
public IEnumerator ImplicitOperator_ConvertsToJAction() => UniTask.ToCoroutine(async () =>
281285
{
282-
var action = JAction.Create().Do(() => { });
286+
// Use using to ensure action is disposed even if ExecuteAsync throws
287+
using var action = JAction.Create().Do(() => { });
283288
var handle = action.ExecuteAsync();
284289

285290
JAction converted = handle;
286291

287292
Assert.AreSame(action, converted);
288293

289294
await handle;
290-
action.Dispose();
291295
});
292296

293297
[Test]

0 commit comments

Comments
 (0)