fluent / fluent-logger-node

A structured logger for Fluentd (Node.js)
Apache License 2.0
259 stars 83 forks source link

Logger write to socket after it died #149

Closed nbvehbq closed 5 years ago

nbvehbq commented 5 years ago

Look at my log:

Fluentd error { Error: read ETIMEDOUT
     at TCP.onread (net.js:622:25) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'read' }
Fluentd will reconnect after 5 seconds
/app/node_modules/fluent-logger/lib/sender.js:395
    this._socket.write(packet, () => {
                 ^

TypeError: Cannot read property 'write' of null
    at FluentSender._doWrite (/app/node_modules/fluent-logger/lib/sender.js:395:18)
    at FluentSender._doFlushSendQueue (/app/node_modules/fluent-logger/lib/sender.js:389:12)
    at Timeout._flushSendQueueTimeoutId.setTimeout [as _onTimeout] (/app/node_modules/fluent-logger/lib/sender.js:348:18)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5) 

I think this is because _doFlushSendQueue () is called as a callback in setTimeout but we checkthis._socket early but should in the callback or in the _doWrite

_waitToWrite() {
    if (!this._socket) {
      this._flushingSendQueue = false;
      return;
    }

    if (this._socket.writable) {
      if (this._eventMode === 'Message') {
        this._doFlushSendQueue();
      } else {
        if (this._sendQueueSize >= this._sendQueueSizeLimit) {
          this._flushSendQueueTimeoutId && clearTimeout(this._flushSendQueueTimeoutId);
          this._doFlushSendQueue();
        } else {
          this._flushSendQueueTimeoutId && clearTimeout(this._flushSendQueueTimeoutId);
          this._flushSendQueueTimeoutId = setTimeout(() => {
            // should check socket again or may be check in _doWrite
            this._doFlushSendQueue();
          }, this._flushInterval);
        }
      }
    } else {
      process.nextTick(() => {
        this._waitToWrite();
      });
    }
  }
okkez commented 5 years ago

Could you create a new PR to resolve your issue?