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

ext/node(http): "close" event not emitted on a response when client terminates #24324

Open
satyarohith opened this issue Jun 24, 2024 · 0 comments
Labels
bug Something isn't working correctly node compat

Comments

@satyarohith
Copy link
Member

Example:

import http from 'node:http';
import net from 'node:net';
import assert from 'node:assert';

// Create an HTTP server
const server = http.createServer((req, res) => {
  // Flag to check if close event is emitted
  let closeEventEmitted = false;

  // Listen for the 'close' event on the response
  res.on('close', () => {
    console.log('Response close event emitted');
    closeEventEmitted = true;
  });

  // Stream response
  res.writeHead(200, { 'Content-Type': 'text/plain' });

  const interval = setInterval(() => {
    res.write('Hello, world!\n');
  }, 100);

  req.on('end', () => {
    clearInterval(interval);
    res.end();
  });

  req.on('error', (err) => {
    console.error('Request error:', err);
    clearInterval(interval);
    res.end();
  });

  // Verify if close event was emitted when the server is closed
  server.on('close', () => {
    assert.strictEqual(closeEventEmitted, true, 'Close event was not emitted on the response');
    console.log('Server closed and close event was verified');
  });
});

// Start the server
server.listen(3000, () => {
  console.log('Server is listening on port 3000');

  // Create a client connection to the server
  const client = net.createConnection({ port: 3000 }, () => {
    console.log('Client connected to server');

    // Write data to the server
    client.write('GET / HTTP/1.1\r\n');
    client.write('Host: localhost\r\n');
    client.write('Connection: close\r\n');
    client.write('\r\n');

    // End the client connection prematurely while reading data
    client.on('data', (data) => {
      console.log('Client received data:', data.toString());
      client.end();
      console.log('Client connection ended prematurely');
    });
  });

  // Set a timeout to give the server time to handle the premature connection end
  setTimeout(() => {
    // Close the server after some time
    server.close(() => {
      console.log('Server closed');
    });
  }, 2000);
});

Expected output:

➜  node close_event_on_server_node_test.mjs
Server is listening on port 3000
Client connected to server
Client received data: HTTP/1.1 200 OK
Content-Type: text/plain
Date: Mon, 24 Jun 2024 15:58:23 GMT
Connection: close
Transfer-Encoding: chunked

e
Hello, world!


Client connection ended prematurely
Response close event emitted
Request error: Error: aborted
    at abortIncoming (node:_http_server:806:17)
    at socketOnClose (node:_http_server:800:3)
    at Socket.emit (node:events:532:35)
    at TCP.<anonymous> (node:net:338:12) {
  code: 'ECONNRESET'
}
^C

Current output:

➜  deno run -A close_event_on_server_node_test.mjs
Server is listening on port 3000
Client connected to server
Client received data: HTTP/1.1 200 OK
content-type: text/plain
vary: Accept-Encoding
transfer-encoding: chunked
date: Mon, 24 Jun 2024 15:58:31 GMT

1C
Hello, world!
Hello, world!


Client connection ended prematurely
error: Uncaught (in promise) AssertionError: Close event was not emitted on the response
    at new AssertionError (ext:deno_node/assertion_error.ts:536:11)
    at toNode (node:assert:71:15)
    at Function.strictEqual (node:assert:321:3)
    at ServerImpl.<anonymous> (file:///Users/sr/c/denoland/deno/close_event_on_server_node_test.mjs:36:12)
    at ServerImpl.emit (ext:deno_node/_events.mjs:398:35)
    at node:http:1674:49
    at eventLoopTick (ext:core/01_core.js:168:7)
@satyarohith satyarohith added bug Something isn't working correctly node compat labels Jun 24, 2024
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 node compat
Projects
None yet
Development

No branches or pull requests

1 participant