Skip to content

Conversation

@niels-moller
Copy link

Description

As far as I understand, the file status flag O_NON_BLOCK only affects this process' use of the fd, and other processes that inherit it in some way or the other. But here, the fd isn't used after tty.setraw(slave). Other processes, e.g., tkey-runapp, will open the slave pty by name, and it then gets its own fd and set of file status flags.

(It still seems the slave pty fd needs to be kept open, even though it is unused and leaked after create_pty returns. I'm not sure how to refactor the program to be able to just close this apparently unused fd immediately after .setraw).

Fixes # (issues)

Type of change

Please tick any that are relevant to this PR and remove any that aren't.

  • Bugfix (non breaking change which resolve an issue)
  • Feature (non breaking change which adds functionality)
  • Breaking Change (a change which would cause existing functionality to not work as expected)
  • Documentation (a change to documentation)

Submission checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my changes
  • I have tested and verified my changes on target
  • My changes are well written and CI is passing
  • I have squashed my work to relevant commits and rebased on main for linear history
  • I have added a "Co-authored-by: x" if several people contributed, either pair programming or by squashing commits from different authors.
  • I have updated the documentation where relevant (readme, dev.tillitis.se etc.)
  • QEMU is updated to reflect changes

@dehanj
Copy link
Member

dehanj commented Nov 28, 2025

On macos this seems to be needed.
Not sure on Linux, but a compromise could be to do something like:

   # Set slave to non-blocking
    if sys.platform == "darwin":
        flags = fcntl.fcntl(slave, fcntl.F_GETFL)
        fcntl.fcntl(slave, fcntl.F_SETFL, flags | os.O_NONBLOCK)

You are also right about the leaking fd. Wouldn't it be enough to simply close the fd before the function returns?

...
os.close(slave)
return master, slave_name

Seems to work fine on mine.

@niels-moller
Copy link
Author

On macos this seems to be needed.

Weird, how does it fail?

Not sure on Linux, but a compromise could be to do something like:

I don't think it's worth the hassle with extra complexity for this. If it is actually needed on macos, that might deserve a comment.

...
os.close(slave)
return master, slave_name

I tried that, and it doesn't work for me, the usb mux fails with Error: [Errno 5] Input/output error before my test scripts get a chance to connect. I haven't tried to track down exactly what happens (e.g., using strace), but I suspect it's some operation on the master side of the pty that fails when no process has the corresponding slave open.

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.

2 participants