Skip to content

Quality: OAuth callback listener thread and TCP port leak on timeout#733

Open
kumburovicbranko682-boop wants to merge 1 commit into
jpochyla:mainfrom
kumburovicbranko682-boop:contribai/improve/quality/oauth-callback-listener-thread-and-tcp-p
Open

Quality: OAuth callback listener thread and TCP port leak on timeout#733
kumburovicbranko682-boop wants to merge 1 commit into
jpochyla:mainfrom
kumburovicbranko682-boop:contribai/improve/quality/oauth-callback-listener-thread-and-tcp-p

Conversation

@kumburovicbranko682-boop

Copy link
Copy Markdown

Problem

When listen_for_callback_parameter times out via rx.recv_timeout, the function returns early with an error at step 4, skipping handle.join() at step 5. The spawned thread remains blocked on listener.incoming() (or on a slow read_line) indefinitely. Because the TcpListener is owned by the thread and never dropped, the port stays bound until the process exits. Any subsequent OAuth callback attempt that tries to bind the same port will fail with "address already in use", making the retry path dead.

Severity: medium
File: psst-core/src/oauth.rs

Solution

Store the listener handle and forcibly clean it up on timeout. The simplest fix is to make the thread joinable even in the error path:

Changes

  • psst-core/src/oauth.rs (modified)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced

When `listen_for_callback_parameter` times out via `rx.recv_timeout`, the function returns early with an error at step 4, skipping `handle.join()` at step 5. The spawned thread remains blocked on `listener.incoming()` (or on a slow `read_line`) indefinitely. Because the `TcpListener` is owned by the thread and never dropped, the port stays bound until the process exits. Any subsequent OAuth callback attempt that tries to bind the same port will fail with "address already in use", making the retry path dead.


Affected files: oauth.rs

Signed-off-by: kumburovicbranko682-boop <295886834+kumburovicbranko682-boop@users.noreply.github.com>
@jacksongoode

Copy link
Copy Markdown
Collaborator

Looking now 👀

@jacksongoode

Copy link
Copy Markdown
Collaborator

Please test your PR before submitting, this fails on.

error[E0382]: use of moved value: `tx`
  --> psst-core/src/oauth.rs:68:18
   |
41 |     let (tx, rx) = mpsc::channel::<Result<String, Error>>();
   |          -- move occurs because `tx` has type `Sender<Result<String, Error>>`, which does not implement the `Copy` trait
...
45 |     let handle = std::thread::spawn(move || {
   |                                     ------- value moved into closure here
46 |         for stream in listener.incoming() {
   |         --------------------------------- inside of this loop
...
49 |                     if handle_callback_connection(&mut stream, &tx, parameter_name) {
   |                                                                 -- variable moved due to use in closure
...
68 |             drop(tx);
   |                  ^^ value used here after move
   |
   = note: the full name for the type has been written to '/Users/jackson/GitHub/psst/target/debug/deps/psst_core-b548f2ded70568a0.long-type-18267626894736332982.txt'
   = note: consider using `--verbose` to print the full type name to the console
help: consider cloning the value before moving it into the closure
   |
45 ~     let value = tx.clone();
46 ~     let handle = std::thread::spawn(move || {
47 |         for stream in listener.incoming() {
48 |             match stream {
49 |                 Ok(mut stream) => {
50 ~                     if handle_callback_connection(&mut stream, &value, parameter_name) {
   |

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.

2 participants