Skip to content

Preserve outer timing when timer contexts are nested - #261

Open
FanWu-ai wants to merge 1 commit into
gruns:masterfrom
FanWu-ai:fix-nested-timer-context-oct10
Open

FanWu-ai wants to merge 1 commit into
gruns:masterfrom
FanWu-ai:fix-nested-timer-context-oct10

Conversation

@FanWu-ai

@FanWu-ai FanWu-ai commented Oct 9, 2026

Copy link
Copy Markdown

Reusing the same timer in nested, same-thread with blocks currently overwrites its outer start time. The inner exit then clears it, so the outer exit raises RuntimeError instead of reporting its duration. If the inner block raises, that runtime error also replaces the original exception.

Save enclosing start times while the context manager is active and restore the prior state before reporting each elapsed duration. This also leaves the timer correctly exited when the output callback raises. Ordinary one-shot and sequential use, decorator behavior, and disabled output are unchanged.

Add five deterministic regressions for three-level nesting, exception identity, recovery after a caught inner exception and sequential reuse, disabled output, and a failing output callback. Every added test fails on the unmodified implementation.

Validation against 2e09b0dc674bc4375f34b5254d15569569603ee1 on Linux:

  • Full unchanged suite: 65 passed on Python 3.9.25, 3.10.22, and 3.12.14, no skips.
  • Full patched suite: 70 passed on all three interpreters, no skips.
  • Project-pinned mypy 1.7.1 on the configured Python 3.9 runtime: all five source files pass.
  • Source distribution and wheel build and git diff --check pass.

Other Python versions and operating systems remain for hosted CI; no hosted pass is claimed.

AI assistance was used to prepare the implementation and regression tests.

@FanWu-ai
FanWu-ai marked this pull request as ready for review October 9, 2026 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant