Got: `Error: Premature close` on DELETE requests after upgrading to 10.0.3 (from 9.6.0)

Created on 12 Dec 2019  路  16Comments  路  Source: sindresorhus/got

Describe the bug

We updated from 9.6.0 to 10.0.3 and as soon as it was deployed, we started getting Error: Premature close on most or all of our DELETE requests. The GET and POST requests work fine.

Maybe some cases were missed in 7b2ccb0d69fca9aba27373ab344f97d389d692f2?

  • Node.js version: v12.13.1

Actual behavior

Error: Premature close
  File "internal/streams/end-of-stream.js", line 61, col 15, in IncomingMessage.onclose
  File "events.js", line 210, col 5, in IncomingMessage.emit
  File "domain.js", line 498, col 23, in IncomingMessage.EventEmitter.emit
  File "/home/node/guardian-website/node_modules/@szmarczak/http-timer/dist/index.js", line 40, col 20, in IncomingMessage.origin.emit
    return emit(event, ...args);
  File "_http_client.js", line 363, col 11, in TLSSocket.socketCloseListener
  File "events.js", line 215, col 7, in TLSSocket.emit
  File "domain.js", line 498, col 23, in TLSSocket.EventEmitter.emit
  File "net.js", line 659, col 12, in null.<anonymous>
  File "_tls_wrap.js", line 481, col 7, in TCP.done

Expected behavior

We shouldn't get a Premature close error on successful DELETE requests.

Code to reproduce

  const url = "https://example.com";
  const username = "username";
  const password = "password";
  const body = undefined;

  const resp = await got(url, {
    username,
    password,
    method: "DELETE",
    throwHttpErrors: false,
    responseType: "json",
    json: body ? body : undefined,
  });

Checklist

  • [X] I have read the documentation.
  • [X] I have tried my code with the latest version of Node.js and Got.
bug

All 16 comments

url, username, password and body are missing. Please update your code.

@szmarczak well, I cant provide those but I'll put in dummy values I guess

@szmarczak sorry, I had pasted the code for 9.6. Fixed now.

Yeah, it wont work with those variables. We'll need to find a url we can use to test.

Also, I am unable to reproduce locally on my machine (OS-X). But I see the errors as soon as I deploy to our servers.

Are you sure it's [email protected]?

@szmarczak I was on [email protected] but I dont see anything in 10.0.4 that would affect it.

Are you doing this:

got.on('response', response => {
    response.on('error', ...);
});

Or just applying try/catch on the code provided above?

I see it's Error and not GotError, so is this an unhandled rejection / uncaught exception?

What are the response headers?

Unfortunately, this only happens on production servers and I dont have access to the headers. I haven't been able to reproduce locally or on heroku yet.

I'm able to reproduce it now, will post a fix soon.

@szmarczak great news. thanks for looking into it!

I just tried the latest version 10.2.1. I am still getting errors:

ReadError: premature close
  File "/home/node/guardian-website/node_modules/got/dist/source/as-promise.js", line 48, col 27, in EventEmitter.<anonymous>
    emitError(new errors_1.ReadError(error, options));
  File "internal/process/task_queues.js", line 93, col 5, in processTicksAndRejections
  File "/home/node/guardian-website/node_modules/end-of-stream/index.js", line 53, col 86, in onclosenexttick
    if (readable && !(rs && (rs.ended && !rs.destroyed))) return callback.call(stream, new Error('premature close'));
  File "internal/process/task_queues.js", line 75, col 11, in processTicksAndRejections

Filed #1014

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamestalmage picture jamestalmage  路  3Comments

tkoelpin picture tkoelpin  路  3Comments

joolfe picture joolfe  路  3Comments

dAnjou picture dAnjou  路  3Comments

astoilkov picture astoilkov  路  3Comments