ELIFECYCLE error

Open
#359 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript
Domain
api, backend

Research direction

Start in lib/needle.js at done(err, resp), especially request.removeListener('error', had_error), and compare it with the reported Node.js EventEmitter behavior. Investigate whether a ClientRequest can emit an error after a successful response under heavy traffic. The issue is resolved when this sequence no longer causes an unhandled error after a successful request.

Written by the indexing model from the issue text.

Description

Hi.

I am very rarely experiencing ELIFECYCLE error and I suspect it is caused by a needle.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-06-15T08_41_46_354Z-debug.log
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the xxx-xxx-xxx-xxx@0.0.0 start script.
npm ERR! xxx-xxx-xxx-xxx@0.0.0 start: `node ./server.js`
npm ERR! errno 1
      throw er; // Unhandled 'error' event
    at TLSWrap.onerror (_tls_wrap.js:416:11)
    at TLSSocket._emitTLSError (_tls_wrap.js:888:10)
    at TLSSocket.EventEmitter.emit (domain.js:467:12)
    at TLSSocket.emit (events.js:315:20)
    at TLSSocket.socketErrorListener (_http_client.js:469:9)
Emitted 'error' event on ClientRequest instance at:
Error
      ^
events.js:292
    at TLSWrap.callbackTrampoline (internal/async_hooks.js:131:14)\

Whenever this error appears it is only always after a successful request when using needle. At least the 5 times it happened during the last 2 months every time I got a 200 response just before the crash. I suppose it is possible, that after a successful request an error can still be emitted. Correct me if im wrong.

I do have a very heavy traffic and the reproduction rate is <0.1%

After going deeper in to the source code I found this block in lib/needle.js. I guess this is the part where a successful request is being handled:

  function done(err, resp) {
    if (returned++ > 0)
      return debug('Already finished, stopping here.');

    if (timer) clearTimeout(timer);
    request.removeListener('error', had_error);

    if (callback)
      return callback(err, resp, resp ? resp.body : undefined);

    // NOTE: this event used to be called 'end', but the behaviour was confusing
    // when errors ocurred, because the stream would still emit an 'end' event.
    out.emit('done', err);
  }

Note the line 465: request.removeListener('error', had_error);
This removes the 'error' event listener from http.RequestClient that needle uses.

Could this be that needle removes 'error' event listener and for some unknown reasons http.ClientRequest tries to emit an 'error' afterwards?

Because if so, an aplication is going to crash nonetheless.

Here is a reference to Event Emitter: https://nodejs.org/api/events.html
It states that

If an EventEmitter does not have at least one listener registered for the 'error' event, and an 'error' event is emitted, the error is thrown, a stack trace is printed, and the Node.js process exits.

Thanks in advance

Dominant language
JavaScript
Stars
1.6k
Forks
237
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tomas/needle

All issues in tomas/needle

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.