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

Fix zombie socket and random dropped logs when closing transport (TLS) #140

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/winston-syslog.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const winston = require('winston');
const Transport = require('winston-transport');
const { MESSAGE, LEVEL } = require('triple-beam');

const _noop = () => {};
const _noop = () => { };

// Ensure we have the correct winston here.
if (Number(winston.version.split('.')[0]) < 3) {
Expand Down Expand Up @@ -63,6 +63,7 @@ class Syslog extends Transport {
this.retries = 0;
this.queue = [];
this.inFlight = 0;
this.closing = false;

//
// Merge the options for the target Syslog server.
Expand Down Expand Up @@ -264,15 +265,16 @@ class Syslog extends Transport {
// Closes the socket used by this transport freeing the resource.
//
close() {
this.closing = true;
const max = 6;
let attempt = 0;

const _close = () => {
if (attempt >= max || (this.queue.length === 0 && this.inFlight <= 0)) {
if (this.socket) {
if (this.socket.destroy) {
// https://nodejs.org/api/net.html#net_socket_destroy_exception
this.socket.destroy();
if (this.socket.end) {
// https://nodejs.org/api/net.html#net_socket_end_data_encoding_callback
this.socket.end();
} else if (this.socket.close) {
// https://nodejs.org/api/dgram.html#dgram_socket_close_callback
// https://www.npmjs.com/package/unix-dgram#socketclose
Expand Down Expand Up @@ -402,6 +404,9 @@ class Syslog extends Transport {
//
})
.on('close', () => {
if (this.closing) {
return
}
//
// Attempt to reconnect on lost connection(s), progressively
// increasing the amount of time between each try.
Expand Down