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

Process hangs after Listener closed #4284

Closed
keroxp opened this issue Mar 8, 2020 · 4 comments
Closed

Process hangs after Listener closed #4284

keroxp opened this issue Mar 8, 2020 · 4 comments
Assignees
Labels
bug Something isn't working correctly

Comments

@keroxp
Copy link
Contributor

keroxp commented Mar 8, 2020

At first, this is very complicated situation.

Problem occurred

  • Server process (with Deno.listen()) won't exit even after listener.close() called.
  • There are no active resources except rid=0-2

Code

These are very simple request-response type TCP server and client.

server.ts

async function server() {
  const l = Deno.listen({ port: 4444 });
  let buf = new Uint8Array(4);
  const conn = await l.accept();
  const process = async function* () {
    while (true) {
      console.log("[server] reading");
      // Read request with timeout
      const nr = await Promise.race([
        conn.read(buf),
        new Promise((resolve) => {
          setTimeout(resolve, 100);
        })
      ]);
      if (!nr) {
        console.log("[server] read timeout");    
        conn.close();
        return 
      } else {
        console.log("[server] read done", nr);
        console.log("[server] writing");
        const nw = await conn.write(new Uint8Array([0,1,2,3]));
        console.log("[server] written", nw);
      }
    }
  }
  for await (const _ of process());
  l.close();
  console.log("[server] close", Deno.resources());
}
server();

client.ts

const conn = await Deno.connect({ port: 4444 });
async function reqRes() {
  console.log("[cli] writing");
  const nw = await conn.write(new Uint8Array([0,1,2,3]));
  console.log("[cli] written", nw);
  const buf = new Uint8Array(4);
  console.log("[cli] reading");
  const nr = await conn.read(buf);
  console.log("[cli] read done", nr);  
}
// First request-response: 
await reqRes();
// Second request-response: expect error as conn is closed by server
setTimeout(async () => {
  try {
    await reqRes(); 
  } catch (e) {
    console.error(e);    
  } finally {
    conn.close();
  }
  console.log("[cli] close", Deno.resources());
}, 200);

Logs

server.ts

[server] reading
[server] read done 4
[server] writing
[server] written 4
[server] reading
[server] read timeout
[server] close { 0: "stdin", 1: "stdout", 2: "stderr" }
<- hangs

client.ts

[cli] writing
[cli] written 4
[cli] reading
[cli] read done 4
[cli] writing
[cli] written 4
[cli] reading
ConnectionReset: Connection reset by peer (os error 54)
    at Object.constructError ($deno$/errors.ts:43:13)
    at unwrapResponse ($deno$/dispatch_minimal.ts:59:12)
    at Object.sendAsyncMinimal ($deno$/dispatch_minimal.ts:102:10)
    at async Object.read ($deno$/files.ts:154:17)
    at async reqRes (file:///Users/keroxp/src/deno-pg/conn/client.ts:8:14)
    at async file:///Users/keroxp/src/deno-pg/conn/client.ts:16:5
[cli] close { 0: "stdin", 1: "stdout", 2: "stderr" }
<- exit

Guess

Some async handle by conn.read(buf) won't be released after conn.close() or listener.close() called if it hasn't be resolved. I think Closer should cleanup unresolved IO handle.

@bartlomieju
Copy link
Member

Most likely the same cause as in #3390

@jsouto18
Copy link
Contributor

jsouto18 commented Mar 8, 2020

This happens because the poll_read is stuck on Poll::Pending. This task needs to be woken on StreamResource drop else it will hang forever.

@ry ry added the bug Something isn't working correctly label Mar 9, 2020
@ry
Copy link
Member

ry commented Mar 9, 2020

Assigning to @piscisaureus just to keep track of this. We've discussed this in some detail and came to the conclusion that this will require some work to fix.
First thing is we need a smaller test that involves just listen, accept, read.

@bartlomieju
Copy link
Member

Fixed in #4293

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly
Projects
None yet
Development

No branches or pull requests

5 participants