Mercurial > hg
annotate rust/chg/src/uihandler.rs @ 44755:4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
We no longer need to spawn a task just to keep the pager handle. The pager
handle can be held by ChgUiHandler since the handler itself is not consumed
and recreated across async calls.
Differential Revision: https://phab.mercurial-scm.org/D8449
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 11 Apr 2020 02:51:03 +0900 |
parents | c794d0da5fb2 |
children | 27fe8cc1338f |
rev | line source |
---|---|
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
1 // Copyright 2018 Yuya Nishihara <yuya@tcha.org> |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
2 // |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
3 // This software may be used and distributed according to the terms of the |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
4 // GNU General Public License version 2 or any later version. |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
5 |
44750
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
6 use async_trait::async_trait; |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
7 use std::io; |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
8 use std::os::unix::io::AsRawFd; |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
9 use std::os::unix::process::ExitStatusExt; |
44737
e9e44e61042b
rust-chg: upgrade to futures-0.3 based libraries
Yuya Nishihara <yuya@tcha.org>
parents:
44689
diff
changeset
|
10 use std::process::Stdio; |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
11 use tokio; |
44755
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
12 use tokio::process::{Child, ChildStdin, Command}; |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
13 |
44689
6bef9d43cc55
rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents:
43818
diff
changeset
|
14 use crate::message::CommandSpec; |
6bef9d43cc55
rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents:
43818
diff
changeset
|
15 use crate::procutil; |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
16 |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
17 /// Callback to process shell command requests received from server. |
44750
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
18 #[async_trait] |
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
19 pub trait SystemHandler { |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
20 type PagerStdin: AsRawFd; |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
21 |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
22 /// Handles pager command request. |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
23 /// |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
24 /// Returns the pipe to be attached to the server if the pager is spawned. |
44750
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
25 async fn spawn_pager(&mut self, spec: &CommandSpec) -> io::Result<Self::PagerStdin>; |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
26 |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
27 /// Handles system command request. |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
28 /// |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
29 /// Returns command exit code (positive) or signal number (negative). |
44750
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
30 async fn run_system(&mut self, spec: &CommandSpec) -> io::Result<i32>; |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
31 } |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
32 |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
33 /// Default cHg implementation to process requests received from server. |
44755
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
34 pub struct ChgUiHandler { |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
35 pager: Option<Child>, |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
36 } |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
37 |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
38 impl ChgUiHandler { |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
39 pub fn new() -> ChgUiHandler { |
44755
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
40 ChgUiHandler { pager: None } |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
41 } |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
42 |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
43 /// Waits until the pager process exits. |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
44 pub async fn wait_pager(&mut self) -> io::Result<()> { |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
45 if let Some(p) = self.pager.take() { |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
46 p.await?; |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
47 } |
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
48 Ok(()) |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
49 } |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
50 } |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
51 |
44750
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
52 #[async_trait] |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
53 impl SystemHandler for ChgUiHandler { |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
54 type PagerStdin = ChildStdin; |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
55 |
44750
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
56 async fn spawn_pager(&mut self, spec: &CommandSpec) -> io::Result<Self::PagerStdin> { |
44737
e9e44e61042b
rust-chg: upgrade to futures-0.3 based libraries
Yuya Nishihara <yuya@tcha.org>
parents:
44689
diff
changeset
|
57 let mut pager = new_shell_command(&spec).stdin(Stdio::piped()).spawn()?; |
e9e44e61042b
rust-chg: upgrade to futures-0.3 based libraries
Yuya Nishihara <yuya@tcha.org>
parents:
44689
diff
changeset
|
58 let pin = pager.stdin.take().unwrap(); |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
59 procutil::set_blocking_fd(pin.as_raw_fd())?; |
40119
e70b616a077b
rust-chg: remove SIGCHLD handler which won't work in oxidized chg
Yuya Nishihara <yuya@tcha.org>
parents:
39974
diff
changeset
|
60 // TODO: if pager exits, notify the server with SIGPIPE immediately. |
e70b616a077b
rust-chg: remove SIGCHLD handler which won't work in oxidized chg
Yuya Nishihara <yuya@tcha.org>
parents:
39974
diff
changeset
|
61 // otherwise the server won't get SIGPIPE if it does not write |
e70b616a077b
rust-chg: remove SIGCHLD handler which won't work in oxidized chg
Yuya Nishihara <yuya@tcha.org>
parents:
39974
diff
changeset
|
62 // anything. (issue5278) |
e70b616a077b
rust-chg: remove SIGCHLD handler which won't work in oxidized chg
Yuya Nishihara <yuya@tcha.org>
parents:
39974
diff
changeset
|
63 // kill(peerpid, SIGPIPE); |
44755
4b0185841058
rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents:
44750
diff
changeset
|
64 self.pager = Some(pager); |
44750
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
65 Ok(pin) |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
66 } |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
67 |
44750
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
68 async fn run_system(&mut self, spec: &CommandSpec) -> io::Result<i32> { |
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
69 let status = new_shell_command(&spec).spawn()?.await?; |
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
70 // TODO: unindent |
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
71 { |
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
72 { |
43818
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40119
diff
changeset
|
73 let code = status |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40119
diff
changeset
|
74 .code() |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40119
diff
changeset
|
75 .or_else(|| status.signal().map(|n| -n)) |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
76 .expect("either exit code or signal should be set"); |
44750
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
77 Ok(code) |
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
78 } |
c794d0da5fb2
rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents:
44737
diff
changeset
|
79 } |
39974
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
80 } |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
81 } |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
82 |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
83 fn new_shell_command(spec: &CommandSpec) -> Command { |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
84 let mut builder = Command::new("/bin/sh"); |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
85 builder |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
86 .arg("-c") |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
87 .arg(&spec.command) |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
88 .current_dir(&spec.current_dir) |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
89 .env_clear() |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
90 .envs(spec.envs.iter().cloned()); |
a9c5fc436fd5
rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
91 builder |
43818
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40119
diff
changeset
|
92 } |