Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for closing Channel and yielding socket file descriptor (without closing the fd) #2820

Open
weissi opened this issue Jul 31, 2024 · 0 comments

Comments

@weissi
Copy link
Member

weissi commented Jul 31, 2024

Sometimes I need to write integration tests where I basically just need a connected socket (without doing anything to it). But somewhat later I want to hand this file descriptor into a library which then does networking with the already-open socket.

The only way I can achieve this today is:

  1. Use socket+connect to get a raw socket
  2. ... do test setup ...
  3. Use library (or ClientBootstrap.withConnectedSocket(...)) to then kick off the actual processing

It's annoying that I can't currently use SwiftNIO to perform step 1. I mean I can easily create a socket and not do anything with it, but I can't get the file descriptor of the underlying socket. What I'd love to do is something like

let channel = try ClientBootstrap(...)
   .connect(host: ..., port: ...)
   .wait()
let fileDescriptorPromise = channel.eventLoop.makePromise(of: CInt.self)
try channel.triggerOutboundUserEvent(SpecialCloseChannelAndGiveMeTheSocketFD(fileDescriptorPromise)).wait()
let fd = try fileDescriptorPromise.futureResult.wait()

[...]

let channelAgain = try ClientBootstrap(...)
    .channelInitializer { channel in
        // actual setup
    }
    .withConnectedSocket(fd)
    .wait()

The semantic I would want is: Do everything exactly like a close(mode: .all), except when NIO would normally call close(fd), don't do that and provide me with the (now unmanaged by NIO) fd.

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

No branches or pull requests

1 participant