Add SSH support to NIO project

Hi everyone!

(Background: I want to use Swift NIO SSH to add SSH support to a text based NIO application)

The example programs "NIOSSHServer" and "NIOSSHClient" give these errors when they connect to each other:

NIOCore/SystemCallHelpers.swift:61: Precondition failed: unacceptable errno 9 Bad file descriptor in close(descriptor:))

2023-01-16 16:48:16.722856+0100 NIOSSHServer[24190:530412] NIOCore/SystemCallHelpers.swift:61: Precondition failed: unacceptable errno 9 Bad file descriptor in close(descriptor:))


My questions are:

Thanks in advance!

Kind regards,

Maarten Engels

These errors are fatal and loud for a reason. EBADF implies that the file descriptor we passed to close is not valid any longer. That means someone else closed a file descriptor that NIO believes it owns. This must always be immediately fatal, as it's impossible to recover from: the program is in a non-deterministic state.

In your case, are you handling file descriptors anywhere? Are you calling close on any, either directly or through Foundation's FileHandle or Pipe?

Hi Cory,

I'm just running the built in examples in the Swift NIO SSH repo (NIOSSHServer and NIOSSHClient). So that would mean the bug is somewhere in there. Maybe I should create an issue there in GitHub?

Kind regards,

Maarten Engels

Yeah, that looks like a bug. I think fixing it should be as straightforward as calling dup on the two file descriptors here before we pass them to the withPipes call. Want to try that and see if it fixes your issue?

I'll give it a try tonight! And if it works, I'll submit it as a PR to the repo.

This works! And as the tests succeed, I created a PR: fix lost file handle error by maartene · Pull Request #129 · apple/swift-nio-ssh · GitHub

(also, I'm making some progress using Swift NIO SSH now)

:tada: I got it working! Swift NIO SSH is a lot easier to use than I initially expected. Thank you for the great work on this library. Making something as complicated as SSH easy to use is definitely a great achievement. :pray:

(ICYMI: the GitHub link in the original post points to the result)

5 Likes

Delighted to hear it! Please do file any further bugs you might hit.

1 Like