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

fix(external): close stdin properly in shutdown() #255

Merged
merged 4 commits into from
Feb 18, 2025

Conversation

flaneur2020
Copy link
Contributor

@flaneur2020 flaneur2020 commented Feb 18, 2025

it seems that we can not pass EOF to child process's stdin by shutdown().

as the doc from tokio described, shutdown() will fire a shutdown(Write) syscall to the socket. but this syscall does not works for file descriptors other than socket.

        /// Shuts down the output stream, ensuring that the value can be dropped
        /// cleanly.
        ///
        /// Equivalent to:
        ///
        /// ```ignore
        /// async fn shutdown(&mut self) -> io::Result<()>;
        /// ```
        ///
        /// Similar to [`flush`], all intermediately buffered is written to the
        /// underlying stream. Once the operation completes, the caller should
        /// no longer attempt to write to the stream. For example, the
        /// `TcpStream` implementation will issue a `shutdown(Write)` sys call.
        ///
        /// [`flush`]: fn@crate::io::AsyncWriteExt::flush

to real close() the fd to pass the eof properly to the child process, we have to drop() this stdin to close it.

i made an experiment in: https://gist.github.com/flaneur2020/4e74d7c8acc6b8dbe41cacb28c131dba . this program will keep hangs with shutdown(), and will runs as expected with drop().

this pr fixed the shutdown() to drop() to close the file descriptor properly.

Signed-off-by: Li Yazhou <[email protected]>
Copy link
Member

@xxchan xxchan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Just curious where are you using the external engine?

@xxchan
Copy link
Member

xxchan commented Feb 18, 2025

Do you want to bump version (and CHANGELOG) now? I can publish a new release for you.

@flaneur2020
Copy link
Contributor Author

Thanks for the fix! Just curious where are you using the external engine?

i am trying to maintain a small set of a smoke test suite for my cloud service, this test suite is not planned to be open sourced and won't become big, so i guess it might be a good choice to use the external engine for this use case.

@flaneur2020
Copy link
Contributor Author

Do you want to bump version (and CHANGELOG) now? I can publish a new release for you.

thank you a lot! let me update it : )

Signed-off-by: Li Yazhou <[email protected]>
Comment on lines -117 to +121
self.stdin.shutdown().await.ok();
drop(self.stdin.take());
Copy link
Member

@xxchan xxchan Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just found the implementation of shutdown for ChildStdio is just Poll::Ready(Ok(())). So the problem is drop not called, not because of shutdown called.

Comment on lines 101 to 104
match &mut self.stdin {
Some(stdin) => stdin.write_all(input.as_bytes()).await?,
None => return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into()),
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we directly panic if it's already shut down, just like

&self.conn.as_ref().expect("connection is shutdown").0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense, updated it in 6a05348, PTAL

flaneur2020 and others added 2 commits February 18, 2025 18:21
Signed-off-by: xxchan <[email protected]>
@xxchan xxchan enabled auto-merge (squash) February 18, 2025 12:35
@xxchan xxchan merged commit 6660c7a into risinglightdb:main Feb 18, 2025
4 checks passed
@xxchan
Copy link
Member

xxchan commented Feb 18, 2025

0.27.2 published

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.

3 participants