Skip to content

Hook before timeout exception is started by calling interrupt()Β #2938

Description

@frankbenoit

The problem: When having sporadic timeout exceptions, it is hard to tell from the stacktrace where the code was hanging.
A debugger might not be available (sporadic, on build system)
Also the stackdump of other threads might be very helpful, because the timeout might be caused by other threads activity or dead lock.

An own extension seems to be not reasonable, as the timeout control also from maven, is part of the infrastructure.

Deliverables

  • Either have a hook, that gets called before the thread.interrupt() is called in org.junit.jupiter.engine.extension.TimeoutInvocation.InterruptTask.run().
  • Or have a way to activate collecting a stackdump of all threads (just before calling interrupt() as above) and put it into the TimeoutException.
  • Or have collected stackdumps of all threads as information always in the TimeoutException

Activity

  1. khmarbaise commented on Jul 30, 2022

    @khmarbaise

    Can you please add more information how your pom file looks like, which Maven versions, JDK version etc. you are using... Also which surefire-plugin versions etc. ?

  2. frankbenoit commented on Jul 30, 2022

    @frankbenoit
    Author

    maven 3.8.6,
    surefire 2.22.2,
    JDK 17

    To make the problem perhaps even better understandable...
    Given a test running code that triggers parallel workers and somehow waits for completion. If then the task (in test-thread) gets interrupted due to timeout, it might catch the InterruptedException and force the parallel workers to be closed before rethrowing. So at the time, the timeout exception can be caught from a live cycle method, it might be already too late to see the status of the parallel workers right before the interruption.

    Hence I think it would be very helpful to have a hook that runs just before the thread.interrupt() call.
    Having the all-thread-dump from JUnit directly would be helpful, but I can also image that a screenshot or other actions can be helpful.

    The location of the interrupt call is meanwhile here: https://github.com/junit-team/junit5/blob/main/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/SameThreadTimeoutInvocation.java#L79

  3. axelfontaine commented on Mar 5, 2023

    @axelfontaine

    +1 here

    This is not Maven-specific.

    Something like @Timeout(value = 600, threadDump = true) would cover IDEs, Maven, Gradle and whatnot in one go.

    Here is a proposed solution: https://stackoverflow.com/a/73156213/350428

    Integrating this directly into JUnit itself would make it accessible to a much wider audience.

  4. marcphilipp commented on Jun 30, 2023

    @marcphilipp
    Member

    Team decision: Add a PreInterruptCallback Extension API and a default implementation that prints a thread dump of the current JVM to System.out if a new Jupiter-specific configuration parameter is set to true.

  5. marcphilipp commented on Jun 30, 2023

    @marcphilipp
    Member

    @frankbenoit Would you be interested in submitting a PR for this?

  6. frankbenoit commented on Jul 1, 2023

    @frankbenoit
    Author

    @marcphilipp no, sorry, not at the moment

  7. 9 remaining items

  8. added this to the 5.12 M1 milestone on Oct 17, 2024
  9. added a commit that references this issue on Oct 31, 2024
    f32aa0a
  10. github-actions commented on May 4, 2026

    @github-actions

    This issue was resolved and the conversation been locked due to inactivity.

    The JUnit team generally speaking doesn't look at resolved issues. If you do have a similar request or problem, please create a new issue. You can include a reference to this issue to provide additional context.

  11. locked as resolved and limited conversation to collaborators on May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions