Skip to content

CliRunner terminates with io.UnsupportedOperation when using faulthandler #2865

Description

@ThomasLoke

It looks like the output redirection for CliRunner doesn't play nicely with Python's faulthandler library, as it terminates prematurely with the following backtrace (slightly modified to hide local filesystem details):

Traceback (most recent call last):
  File "$CONDA_PREFIX/lib/python3.11/site-packages/click/testing.py", line 408, in invoke
    return_value = cli.main(args=args or (), prog_name=prog_name, **extra)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$CONDA_PREFIX/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "$CONDA_PREFIX/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$CONDA_PREFIX/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./test_click.py", line 12, in main
    faulthandler.enable()
io.UnsupportedOperation: fileno

Code for test_click.py that replicates the problem:

import click
from click.testing import CliRunner
import faulthandler
import traceback

@click.command()
@click.option('--flag', type=bool, default=True)
def main(flag):
    print("Executing main function...")
    if flag:
      print("Registering faulthandler")
      faulthandler.enable()
    print("Finished executing main function.")

if __name__ == '__main__':
    flag_value = True
    runner = CliRunner()
    result = runner.invoke(main, ["--flag", flag_value])
    print("Exit code = %d; contents of stdout:\n%s" % (result.exit_code, result.stdout))
    if result.exit_code != 0:
        traceback.print_exception(result.exc_info[1])

The code was then executed by running python test_click.py

I assume the problem is that the CliRunner redirected stderr doesn't implement some details that are required by the faulthandler signal handler registration. Ideally, it should just work; or at least, it should avoid crashing and just do nothing (and perhaps be documented somewhere).

Environment:

  • Python version: 3.11.10
  • Click version: 8.1.7

Activity

  1. added this to the 8.2.1 milestone on May 10, 2025
  2. modified the milestones: 8.2.1, 8.2.2 on May 14, 2025
  3. modified the milestones: 8.2.2, on Jul 16, 2025
  4. removed this from the milestone on Aug 3, 2025
  5. added a commit that references this issue on Mar 4, 2026
    b8eed81
  6. kdeldycke commented on Mar 4, 2026

    @kdeldycke
    Collaborator

    @ThomasLoke I proposed a fix in #3244 , can you test it?

  7. added
    test runner
    on Mar 4, 2026
  8. added this to the 8.3.2 milestone on Mar 4, 2026
  9. changed the title [-]CliRunner terminates with io.UnsupportedOperation when using faulthandler[/-] [+]`CliRunner` terminates with `io.UnsupportedOperation` when using `faulthandler`[/+] on Mar 4, 2026
  10. added a commit that references this issue on Mar 4, 2026
    7de8209
  11. ThomasLoke commented on Mar 5, 2026

    @ThomasLoke
    Author

    @kdeldycke Took it for a spin, and it works for me! And from a quick peek of the PR, the changes make sense to me, so all good from my side :)

  12. kdeldycke commented on Mar 5, 2026

    @kdeldycke
    Collaborator

    Thanks a lot @ThomasLoke for the review and test! Your feedback is valuable. Thanks for your patience! :)

  13. abhii734 commented on Mar 9, 2026

    @abhii734
  14. kdeldycke commented on Mar 9, 2026

    @kdeldycke
    Collaborator

    I'd like to work on this bug. Let me investigate and submit a fix.

    You did not read the thread: a fix has already been proposed at #3244 and validated by the original reporter.

  15. modified the milestones: 8.3.2, 8.3.3 on Mar 15, 2026
  16. added 3 commits that reference this issue on Apr 8, 2026
    02ee20a
    726d3b1
    8762c77
  17. kdeldycke commented on Apr 16, 2026

    @kdeldycke
    Collaborator

    #3244 has been merged upstream and will be part of the next 8.3.3 release.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    test runner

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions