From c53aa38b897a00af93df16d20ac60ad3d486762b Mon Sep 17 00:00:00 2001 From: Nanaloveyuki Date: Sun, 14 Jun 2026 02:17:23 +0800 Subject: [PATCH] :white_check_mark: cover async shutdown failure path --- docs/api/async-logger-shutdown.md | 3 ++ docs/api/library-async-logger-shutdown.md | 1 + src-async/BitLoggerAsync_test.mbt | 43 +++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/docs/api/async-logger-shutdown.md b/docs/api/async-logger-shutdown.md index 1809324..1eb8d7f 100644 --- a/docs/api/async-logger-shutdown.md +++ b/docs/api/async-logger-shutdown.md @@ -39,6 +39,7 @@ Detailed rules explaining key parameters and behaviors - `clear=true` immediately closes and abandons pending records. - In runtimes where shutdown waits for workers, the method then waits until `is_running()` becomes `false` before returning. - In the current backend split, native-worker runtimes enable both the post-`wait_idle()` clear fallback and the final wait-for-worker phase, while compatibility runtimes skip both extra steps. +- That means a failure-short-circuited `wait_idle()` can still be followed by forced pending-to-dropped cleanup on native-worker runtimes, while compatibility runtimes close without that extra forced clear step. - Because `clear=false` delegates to `wait_idle()` first, shutdown can also wait indefinitely when pending records exist but no worker is making progress and no failure flag is raised. ### How to Use @@ -70,6 +71,8 @@ e.g.: - If `wait_idle()` returns early because the worker failed, shutdown behavior after that point still depends on the active runtime's fallback and worker-wait rules. +- After a worker failure, native-worker shutdown may convert the remaining backlog into dropped records, while compatibility shutdown can leave the pending counter reflecting that leftover closed queue state. + - In compatibility-style runtimes without background-worker waiting, shutdown still closes the logger but may not perform the extra wait-for-worker phase described for native-worker runtimes. - If pending work exists but no worker was started, `shutdown(clear=false)` may never reach its later close step because it is still waiting inside `wait_idle()`. diff --git a/docs/api/library-async-logger-shutdown.md b/docs/api/library-async-logger-shutdown.md index 66933e0..275ebd5 100644 --- a/docs/api/library-async-logger-shutdown.md +++ b/docs/api/library-async-logger-shutdown.md @@ -42,6 +42,7 @@ Detailed rules explaining key parameters and behaviors - If the active async runtime uses shutdown clearing after idle and backlog still remains, the wrapped logger falls back to `close(clear=true)`. - `clear=true` immediately closes and abandons pending records. - In runtimes where shutdown waits for workers, the method then waits until the worker is no longer running before returning. +- After a worker-failure short-circuit, native-worker backends can still convert remaining backlog into dropped records, while compatibility backends skip that extra forced-clear step. - The narrower library facade does not change any of these runtime-dependent shutdown rules; it only keeps the broader inspection helpers out of the direct public surface. ### How to Use diff --git a/src-async/BitLoggerAsync_test.mbt b/src-async/BitLoggerAsync_test.mbt index e9fee5c..c5b51b7 100644 --- a/src-async/BitLoggerAsync_test.mbt +++ b/src-async/BitLoggerAsync_test.mbt @@ -406,6 +406,49 @@ async test "later started run resets async failure state before draining remaini inspect(logger.pending_count(), content="0") } +async test "shutdown after worker failure uses runtime-specific pending cleanup" { + let writes : Ref[Int] = Ref(0) + let logger = async_logger( + @bitlogger.callback_sink(fn(_) { + writes.val += 1 + }), + config=AsyncLoggerConfig::new( + max_pending=4, + overflow=AsyncOverflowPolicy::Blocking, + flush=AsyncFlushPolicy::Batch, + ), + min_level=@bitlogger.Level::Info, + target="async.failure.shutdown", + flush=fn(_) -> Int raise { + raise TestFlushError("flush exploded on shutdown path") + }, + ) + + @async.with_task_group(group => { + group.spawn_bg(allow_failure=true, () => logger.run()) + logger.info("one") + logger.info("two") + logger.wait_idle() + inspect(logger.has_failed(), content="true") + inspect(logger.pending_count(), content="1") + logger.shutdown() + }) + + inspect(logger.is_closed(), content="true") + inspect(logger.has_failed(), content="true") + inspect(logger.last_error().contains("TestFlushError"), content="true") + inspect(logger.is_running(), content="false") + inspect(writes.val, content="1") + inspect( + logger.pending_count(), + content=if async_runtime_supports_background_worker() { "0" } else { "1" }, + ) + inspect( + logger.dropped_count(), + content=if async_runtime_supports_background_worker() { "1" } else { "0" }, + ) +} + async test "library async logger keeps a smaller async facade" { let written_targets : Ref[Array[String]] = Ref([]) let written_messages : Ref[Array[String]] = Ref([])