Skip to content

Commit a0bdff6

Browse files
committed
refactor: use process_group(0) and consolidate shutdown logic
1 parent 2bc7ee2 commit a0bdff6

File tree

6 files changed

+45
-45
lines changed

6 files changed

+45
-45
lines changed

examples/function_router/src/components/pagination.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ pub fn Links(props: &Props) -> Html {
131131
page, total_pages, ..
132132
} = *props;
133133

134-
let pages_prev = page.checked_sub(1).unwrap_or_default() as usize;
134+
let pages_prev = page.saturating_sub(1) as usize;
135135
let pages_next = (total_pages - page) as usize;
136136

137137
let links_left = LINKS_PER_SIDE.min(pages_prev)
138138
// if there are less than `LINKS_PER_SIDE` to the right, we add some more on the left.
139-
+ LINKS_PER_SIDE.checked_sub(pages_next).unwrap_or_default();
139+
+ LINKS_PER_SIDE.saturating_sub(pages_next);
140140
let links_right = 2 * LINKS_PER_SIDE - links_left;
141141

142142
html! {

examples/keyed_list/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ impl Component for App {
106106
true
107107
}
108108
Msg::SortById => {
109-
self.persons
110-
.sort_unstable_by(|a, b| a.info().id.cmp(&b.info().id));
109+
self.persons.sort_unstable_by_key(|a| a.info().id);
111110
true
112111
}
113112
Msg::SortByName => {

examples/router/src/components/pagination.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ impl Pagination {
9595
page, total_pages, ..
9696
} = *props;
9797

98-
let pages_prev = page.checked_sub(1).unwrap_or_default() as usize;
98+
let pages_prev = page.saturating_sub(1) as usize;
9999
let pages_next = (total_pages - page) as usize;
100100

101101
let links_left = LINKS_PER_SIDE.min(pages_prev)
102102
// if there are less than `LINKS_PER_SIDE` to the right, we add some more on the left.
103-
+ LINKS_PER_SIDE.checked_sub(pages_next).unwrap_or_default();
103+
+ LINKS_PER_SIDE.saturating_sub(pages_next);
104104
let links_right = 2 * LINKS_PER_SIDE - links_left;
105105

106106
html! {

packages/yew-macro/src/html_tree/tag.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,12 @@ impl TagTokens {
107107
let next = input.parse()?;
108108
if let TokenTree::Punct(punct) = &next {
109109
match punct.as_char() {
110-
'/' => {
111-
if angle_count == 1 && input.peek(Token![>]) {
112-
div = Some(syn::token::Slash {
113-
spans: [punct.span()],
114-
});
115-
gt = input.parse()?;
116-
break;
117-
}
110+
'/' if angle_count == 1 && input.peek(Token![>]) => {
111+
div = Some(syn::token::Slash {
112+
spans: [punct.span()],
113+
});
114+
gt = input.parse()?;
115+
break;
118116
}
119117
'>' => {
120118
angle_count = angle_count.checked_sub(1).ok_or_else(|| {

packages/yew/src/dom_bundle/btag/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,20 @@ impl Reconcilable for VTag {
175175
match bundle {
176176
// If the ancestor is a tag of the same type, don't recreate, keep the
177177
// old tag and update its attributes and children.
178-
BNode::Tag(ex) if self.key == ex.key => {
179-
if match (&self.inner, &ex.inner) {
180-
(VTagInner::Input(_), BTagInner::Input(_)) => true,
181-
(VTagInner::Textarea { .. }, BTagInner::Textarea { .. }) => true,
182-
(VTagInner::Other { tag: l, .. }, BTagInner::Other { tag: r, .. })
183-
if l == r =>
184-
{
185-
true
186-
}
187-
_ => false,
188-
} {
189-
return self.reconcile(root, parent_scope, parent, slot, ex.deref_mut());
190-
}
178+
BNode::Tag(ex)
179+
if self.key == ex.key
180+
&& match (&self.inner, &ex.inner) {
181+
(VTagInner::Input(_), BTagInner::Input(_)) => true,
182+
(VTagInner::Textarea { .. }, BTagInner::Textarea { .. }) => true,
183+
(VTagInner::Other { tag: l, .. }, BTagInner::Other { tag: r, .. })
184+
if l == r =>
185+
{
186+
true
187+
}
188+
_ => false,
189+
} =>
190+
{
191+
return self.reconcile(root, parent_scope, parent, slot, ex.deref_mut());
191192
}
192193
_ => {}
193194
};

tools/ssr-e2e/src/main.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::process::ExitCode;
22
use std::time::Duration;
33

44
use clap::Parser;
5-
use tokio::process::Command;
5+
use tokio::process::{Child, Command};
66
use tokio::time::{sleep, Instant};
77

88
#[derive(Parser)]
@@ -88,15 +88,24 @@ async fn wait_for_server(url: &str, timeout: Duration) -> bool {
8888
false
8989
}
9090

91-
#[cfg(unix)]
92-
fn kill_process_group(id: u32) {
93-
unsafe {
94-
libc::kill(-(id as i32), libc::SIGTERM);
91+
/// Terminates the server and all its descendant processes.
92+
///
93+
/// The server is started via `sh -c "..."`, producing a process tree
94+
/// (sh -> cargo run -> server binary). `Child::kill()` alone would only
95+
/// kill `sh`, orphaning the actual server process on the port. On Unix we
96+
/// use process groups (set up via `process_group(0)` at spawn time) so a
97+
/// single `kill(-pgid, SIGTERM)` reaches the entire tree.
98+
fn shutdown_server(server: &mut Child) {
99+
#[cfg(unix)]
100+
if let Some(id) = server.id() {
101+
unsafe {
102+
libc::kill(-(id as i32), libc::SIGTERM);
103+
}
104+
return;
95105
}
96-
}
97106

98-
#[cfg(not(unix))]
99-
fn kill_process_group(_id: u32) {}
107+
let _ = server.start_kill();
108+
}
100109

101110
#[tokio::main]
102111
async fn main() -> ExitCode {
@@ -116,17 +125,10 @@ async fn main() -> ExitCode {
116125
cmd.stdout(std::process::Stdio::inherit());
117126
cmd.stderr(std::process::Stdio::inherit());
118127
#[cfg(unix)]
119-
unsafe {
120-
cmd.pre_exec(|| {
121-
libc::setpgid(0, 0);
122-
Ok(())
123-
});
124-
}
128+
cmd.process_group(0);
125129
cmd.spawn().expect("failed to start server process")
126130
};
127131

128-
let server_pid = server.id().expect("server has no pid");
129-
130132
eprintln!("[ssr-e2e] Waiting for server at {} ...", args.health_url);
131133

132134
let ready = wait_for_server(&args.health_url, Duration::from_secs(args.timeout)).await;
@@ -135,7 +137,7 @@ async fn main() -> ExitCode {
135137
"[ssr-e2e] Server did not become ready within {}s",
136138
args.timeout
137139
);
138-
kill_process_group(server_pid);
140+
shutdown_server(&mut server);
139141
let _ = server.wait().await;
140142
return ExitCode::FAILURE;
141143
}
@@ -159,7 +161,7 @@ async fn main() -> ExitCode {
159161
.await;
160162

161163
eprintln!("[ssr-e2e] Shutting down server ...");
162-
kill_process_group(server_pid);
164+
shutdown_server(&mut server);
163165
let _ = server.wait().await;
164166

165167
match test_result {

0 commit comments

Comments
 (0)