Skip to content

Commit 146985f

Browse files
authored
fix: reduce references to Server in codex-login crate (#2398)
Updates the tokio task that monitors `shutdown_notify` and server requests to ensure that `server.unblock()` is always called, which means that `ShutdownHandle` only has to invoke `notify_waiters()`. Now `LoginServer` no longer has to maintain a reference to `Server`. The `Arc<Server>` only has two active references: the `thread::spawn()` for reading server messages and the `tokio::task()` that consumes them (and the shutdown message). Now when shutdown is called (or if login completes successfully), the `server.unblock()` call ensures the thread terminates cleanly, which in turn ensures `rx.recv()` in the `tokio::spawn()` returns `Err`, causing the `tokio::task()` to exit cleanly, as well. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/openai/codex/pull/2398). * #2399 * __->__ #2398 * #2396 * #2395 * #2394 * #2393 * #2389
1 parent d5b42ba commit 146985f

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

codex-rs/login/src/server.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,14 @@ impl LoginServer {
6666
}
6767
}
6868

69-
#[derive(Clone)]
69+
#[derive(Clone, Debug)]
7070
pub struct ShutdownHandle {
7171
shutdown_notify: Arc<tokio::sync::Notify>,
72-
server: Arc<Server>,
73-
}
74-
75-
impl std::fmt::Debug for ShutdownHandle {
76-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77-
f.debug_struct("ShutdownHandle")
78-
.field("shutdown_notify", &self.shutdown_notify)
79-
.finish()
80-
}
8172
}
8273

8374
impl ShutdownHandle {
8475
pub fn shutdown(&self) {
8576
self.shutdown_notify.notify_waiters();
86-
self.server.unblock();
8777
}
8878
}
8979

@@ -133,14 +123,14 @@ pub fn run_login_server(
133123
let shutdown_notify = shutdown_notify.clone();
134124
let server = server.clone();
135125
tokio::spawn(async move {
136-
loop {
126+
let result = loop {
137127
tokio::select! {
138128
_ = shutdown_notify.notified() => {
139-
return Err(io::Error::other("Login was not completed"));
129+
break Err(io::Error::other("Login was not completed"));
140130
}
141131
maybe_req = rx.recv() => {
142132
let Some(req) = maybe_req else {
143-
return Err(io::Error::other("Login was not completed"));
133+
break Err(io::Error::other("Login was not completed"));
144134
};
145135

146136
let url_raw = req.url().to_string();
@@ -159,24 +149,24 @@ pub fn run_login_server(
159149
}
160150

161151
if is_login_complete {
162-
shutdown_notify.notify_waiters();
163-
server.unblock();
164-
return Ok(());
152+
break Ok(());
165153
}
166154
}
167155
}
168-
}
156+
};
157+
158+
// Ensure that the server is unblocked so the thread dedicated to
159+
// running `server.recv()` in a loop exits cleanly.
160+
server.unblock();
161+
result
169162
})
170163
};
171164

172165
Ok(LoginServer {
173166
auth_url,
174167
actual_port,
175168
server_handle,
176-
shutdown_handle: ShutdownHandle {
177-
shutdown_notify,
178-
server,
179-
},
169+
shutdown_handle: ShutdownHandle { shutdown_notify },
180170
})
181171
}
182172

0 commit comments

Comments
 (0)