-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Flush stdout buffer when writing data #14
base: main
Are you sure you want to change the base?
Conversation
I'm afraid this isn't a safe change. Introducing new data like this will break some existing communication protocols. However! I do think the underlying problem you discovered should still be fixed. Perhaps we can do it another way? (and yes I 100% agree with your nit, thanks for calling that out!) |
This only applies to when using the process' stdio though. ie other communication protocols (for ex websocket) are not impacted. As an alternative, what do you think of @available(*, deprecated, renamed: "stdio", message: "Use stdio instead")
public static func stdioPipe() -> DataChannel {
stdio(flushOnWrite: false)
}
public static func stdio(flushOnWrite: Bool = true) -> DataChannel { ... } |
That's true! This transport just happens to be really important for my own uses. I think it is critical that transports faithfully write only the data that a user actually intends. I'm much more inclined to use an out-of-band mechanism here like What do you think about keeping non-flushing behavior the default, so existing clients continue to work, and look into using |
I'm not too familiar with fflush, but from what I understood that direction makes sense. How would you incorporate it here? |
Ok, I did little more research, and I think the correct API is probably However, I also believe it is possible that FileHandle may already have an API to do this! https://developer.apple.com/documentation/foundation/filehandle/3172531-synchronize |
Interesting. I tried adding |
So as you initially suggested, what do you think of instead using |
Huh intersting! So, I believe that POSIX error 45 translates to |
it seems that |
Unrelated to this specific PR, as I've been working on JRPC over stdio, I implemented two changes that I wanted to mention in case you'd be interested to have them upstreamed:
|
Ok that's great! Are you up for incorporating it into this PR? It's probably fine to enable this by default, but I think keeping it optional and off by default is the most compatible change. So if you are up for reworking this to use
Yes, I am well-aware of this problem. I have a solution here that works quite well across a number for different shells: https://github.com/ChimeHQ/ProcessEnv. This stuff is definitely a pain, but I think this is pretty independent of JSON-RPC.
Hmm this is an interesting problem! In my use of JSON-RPC, the actual payloads are wrapped in a lower-level protocol that guarantees you know how much data to read before the message is fully-delivered (very similar to HTTP). But I think some kind of The implementation I came up with involves a small wrapper around a custom AsyncSequence, so it looks at least superficially-similar to what you are doing here. |
Given that this is a new function, I enabled flushing by default. This seems to be the most reasonable and the less likely to cause unexpected issues. |
Flush stdout buffer when writing data. I noticed that without an extra line break, the receiver was not getting the message written to stdout.
As a nit (happy to revert) I thought that
stdio
was clearer thatstdioPipe
as afaik piping is specific to shell scripts, and stdin can be written to in other contexts.