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
log4js.shutdown() not working properly - Error [ERR_STREAM_WRITE_AFTER_END]: write after end #1005
Comments
Hello,
The handler defined in handleSignals is then called twice. |
I am not running it in a spawned process. It does have to do with timing though, so there needs to be some heavy logs outputted whilst pressing Ctrl+c. I can unfortunately not share the entire repo as it is a private project. I'd guess the best way to actually check this is the actual source code logic a bit. I am using both a stdout and file appender at the same time (actually, 5 of them at the same time). |
Can you try run this and see what are the listeners that are set for SIGINT?
Besides, could reproduce the issue with a console and a file appender (single file), while using log4js in the following loop and double-hitting
|
@rnd-debug Apologize for replying back so late, I got stuck in a lot of other issues. It still happens. Here is the output. You can notice the [2020-06-01T11:09:30.030+0200] [INFO] service - Some log
^C[2020-06-01T11:09:30.044+0200] [INFO] service - Some log
[2020-06-01T11:09:30.045+0200] [INFO] server - Exiting with code: SIGINT. Gracefully cleaning up, please wait...
(code) => {
logger.info(
`Exiting with code: ${code}. Gracefully cleaning up, please wait...`
);
const listeners = process.listeners('SIGINT'); // <= let's track who is listening
for (let i = 0; i < listeners.length; i++) {
console.log(listeners[i].toString());
}
shutdown(code);
}
(code) => {
logger.info(
`Exiting with code: ${code}. Gracefully cleaning up, please wait...`
);
const listeners = process.listeners('SIGINT'); // <= let's track who is listening
for (let i = 0; i < listeners.length; i++) {
console.log(listeners[i].toString());
}
shutdown(code);
}
log4js.fileAppender - Writing to file /home/userA/code/projectA/log/projectA-server.log, error happened Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:257:14)
at RollingFileStream.Writable.write (_stream_writable.js:306:5)
at RollingFileStream.Writable.end (_stream_writable.js:617:10)
at Function.app.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/appenders/file.js:72:12)
at /home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:59
at Array.forEach (<anonymous>)
at Object.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:44)
at shutdown (/home/userA/code/projectA/server/shutdown.js:21:10)
at process.<anonymous> (/home/userA/code/projectA/server/signals.js:20:7)
at process.emit (events.js:223:5) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
log4js.fileAppender - Writing to file /home/userA/code/projectA/log/projectA-server-config.log, error happened Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:257:14)
at RollingFileStream.Writable.write (_stream_writable.js:306:5)
at RollingFileStream.Writable.end (_stream_writable.js:617:10)
at Function.app.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/appenders/file.js:72:12)
at /home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:59
at Array.forEach (<anonymous>)
at Object.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:44)
at shutdown (/home/userA/code/projectA/server/shutdown.js:21:10)
at process.<anonymous> (/home/userA/code/projectA/server/signals.js:20:7)
at process.emit (events.js:223:5) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
log4js.fileAppender - Writing to file /home/userA/code/projectA/security/projectA-server-audit.log, error happened Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:257:14)
at RollingFileStream.Writable.write (_stream_writable.js:306:5)
at RollingFileStream.Writable.end (_stream_writable.js:617:10)
at Function.app.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/appenders/file.js:72:12)
at /home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:59
at Array.forEach (<anonymous>)
at Object.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:44)
at shutdown (/home/userA/code/projectA/server/shutdown.js:21:10)
at process.<anonymous> (/home/userA/code/projectA/server/signals.js:20:7)
at process.emit (events.js:223:5) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
(code) => {
logger.info(
`Exiting with code: ${code}. Gracefully cleaning up, please wait...`
);
const listeners = process.listeners('SIGINT'); // <= let's track who is listening
for (let i = 0; i < listeners.length; i++) {
console.log(listeners[i].toString());
}
shutdown(code);
}
log4js.fileAppender - Writing to file /home/userA/code/projectA/log/projectA-server.log, error happened Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:257:14)
at RollingFileStream.Writable.write (_stream_writable.js:306:5)
at RollingFileStream.Writable.end (_stream_writable.js:617:10)
at Function.app.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/appenders/file.js:72:12)
at /home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:59
at Array.forEach (<anonymous>)
at Object.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:44)
at shutdown (/home/userA/code/projectA/server/shutdown.js:21:10)
at process.<anonymous> (/home/userA/code/projectA/server/signals.js:20:7)
at process.emit (events.js:223:5) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
log4js.fileAppender - Writing to file /home/userA/code/projectA/log/projectA-server-config.log, error happened Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:257:14)
at RollingFileStream.Writable.write (_stream_writable.js:306:5)
at RollingFileStream.Writable.end (_stream_writable.js:617:10)
at Function.app.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/appenders/file.js:72:12)
at /home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:59
at Array.forEach (<anonymous>)
at Object.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:44)
at shutdown (/home/userA/code/projectA/server/shutdown.js:21:10)
at process.<anonymous> (/home/userA/code/projectA/server/signals.js:20:7)
at process.emit (events.js:223:5) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
log4js.fileAppender - Writing to file /home/userA/code/projectA/security/projectA-server-audit.log, error happened Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:257:14)
at RollingFileStream.Writable.write (_stream_writable.js:306:5)
at RollingFileStream.Writable.end (_stream_writable.js:617:10)
at Function.app.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/appenders/file.js:72:12)
at /home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:59
at Array.forEach (<anonymous>)
at Object.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:44)
at shutdown (/home/userA/code/projectA/server/shutdown.js:21:10)
at process.<anonymous> (/home/userA/code/projectA/server/signals.js:20:7)
at process.emit (events.js:223:5) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
userA@userA-dev ~/code/projectA (log4js) [SIGINT]> |
Well, I've had the same problem. I realized that to close the server you need to close all open connections. Adding this function before http.close() so far works good. |
@rnd-debug @nomiddlename hey did this project die out? |
It's not dead yet, but it's getting obvious to me that I don't really have the time to look after it anymore. I'll put a message up on the readme, and in the slack channel, to see if anyone wants to take it over or at least help out. |
I ran into this as well. To reproduce:
The issue is the double |
Did you get anyone to take over as a new project lead? |
@richardhinkamp is that the exact same issue? From your description, it does not seem like it. In your case you seem to call |
Well it gets to the shutdown() in file.js end does an end() after it has already been ended. As I said, my case was not calling shutdown() twice, but calling shutdown() and after that an unhandled promise rejection which called shutdown() as well. So in my case it was not obvious at first that a double shutdown() had been called. |
Still, is it the same case as mine? I am curious because the PR says it fixes this issue here, which I am not entirely sure it does. Even though you do not manually call |
Hmm looking into the code some more and there are two places where the stream is ended. In shutdown() but also in reopen(), which will be triggered from a SIGHUP, but that one creates a new stream, so that should not result in a ERR_STREAM_WRITE_AFTER_END. So it might be a separate issue, but you need to dig into this. The handling of SIGINT is logged correctly to the console, does it also log to the log file? If that is the case, there might be another signal after the SIGINT, another SIGINT or a SIGTERM, causing another shutdown call. That one won't be logged, since the the shutdown() already disabled all logging. Maybe adding a The real problem is that |
@richardhinkamp the problem I am having is that you are marking this issue as fixed in your PR. So if you mark it as fixed, you will have to make sure it actually fixes this issue and not some other issue you are having. My time is not enough to dig into this code unfortunately, especially with the maintainer being absent for over a year. |
@thernstig I have updated the comment in the PR since this may not fix this. |
Hi @thernstig, I read your reply to @rnd-debug. I noticed that the following logs were written 3 times.
Referencing @rnd-debug's code snippet below, it is possible that the function handleSignals() {
['SIGINT', 'SIGTERM'].forEach((signal) =>
process.on(signal, (code) => {
logger.info( `Exiting with code: ${code}. Gracefully cleaning up, please wait...` );
var listeners = process.listeners('SIGINT'); // <= let's track who is listening
for (var i = 0; i < listeners.length; i++) {
console.log(listeners[i].toString());
}
shutdown(code);
})
);
} From the 3 times occurrence of the logs, it could have been caused by:
For (1), I understand you only pressed My main suspicion will be (2). That would explain why the listener logic was printed 3 times. To ascertain further, can you do me a favor? Use the code below (2 lines changed) and provide the output again. function handleSignals() {
['SIGINT', 'SIGTERM'].forEach((signal) =>
process.on(signal, (code) => {
logger.info( `Exiting with code: ${code}. Gracefully cleaning up, please wait...` );
var listeners = process.listeners(signal); // changed hard-coded 'SIGINT' to signal
for (var i = 0; i < listeners.length; i++) {
console.log(signal, `${i}/${listeners.length}`, listeners[i].toString()); // added signal & listeners.length
}
shutdown(code);
})
);
} Nevertheless, whichever the case, I agree that |
Hi @thernstig and @richardhinkamp, Let me jump the gun here while waiting for @thernstig's results. I found the following code sections with reference to app.shutdown = function (complete) {
process.removeListener('SIGHUP', app.sighupHandler);
writer.end('', 'utf-8', complete);
}; The During multiple calls of The first attempt might have already written Of course, there are other areas in the code that I will not dwell on, such as I shall wait for @thernstig's results first. |
Hi @thernstig, Regardless of multiple As such, I have patched it in PR #1113, by always clearing the appenders when Please help to verify PR #1113 if you can. |
@peteriman this is the output from your proposed function: [2022-01-13T14:41:09.503+0100] [INFO] server - Exiting with code: SIGINT. Gracefully cleaning up, please wait...
SIGINT 0/1 (code) => {
logger.info(
`Exiting with code: ${code}. Gracefully cleaning up, please wait...`
);
const listeners = process.listeners(signal); // changed hard-coded 'SIGINT' to signal
for (let i = 0; i < listeners.length; i++) {
console.log(
signal,
`${i}/${listeners.length}`,
listeners[i].toString()
); // added signal & listeners.length
}
shutdown(code);
}
^CSIGINT 0/1 (code) => {
logger.info(
`Exiting with code: ${code}. Gracefully cleaning up, please wait...`
);
const listeners = process.listeners(signal); // changed hard-coded 'SIGINT' to signal
for (let i = 0; i < listeners.length; i++) {
console.log(
signal,
`${i}/${listeners.length}`,
listeners[i].toString()
); // added signal & listeners.length
}
shutdown(code);
}
log4js.fileAppender - Writing to file /home/userA/code/projectA/devenv_temp/log/projectA-server.log, error happened Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at _write (node:internal/streams/writable:319:11)
at RollingFileStream.Writable.end (node:internal/streams/writable:609:17)
at Function.app.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/appenders/file.js:80:12)
at /home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:59
at Array.forEach (<anonymous>)
at Object.shutdown (/home/userA/code/projectA/server/node_modules/log4js/lib/log4js.js:117:44)
at shutdown (file:///home/userA/code/projectA/server/shutdown.js:24:10)
at process.<anonymous> (file:///home/userA/code/projectA/server/signals.js:23:7)
at process.emit (node:events:390:28) {
code: 'ERR_STREAM_WRITE_AFTER_END'
} In addition @richardhinkamp wrote #1005 (comment):
Could it be the case that one should check https://nodejs.org/api/stream.html#writablewritableended or similar. Since Node.js 10 is EOL I wonder if @nomiddlename would be ok to bump the supported version to version 12. |
@thernstig From the logs, your With that, it calls We could bump it up to Node.js 12, but the fact remains that The general gist of PR #1113 is that on entry of Did you have a chance to try out PR #1113, which resets those appenders?
|
@peteriman I think your logic for all of this is sound, so no objection there. But I am still having a very hard time reproducing this. I cannot even reproduce what I posted in #1005 (comment) now when I try. In my test here you can see Nevertheless, I cannot really re-create this now when I try. I did not test your changes since I could not even reproduce it again without them. How about we merge your changes, I updated, and if I ever see any problem again I report back? In that regard, I am fine to close this issue. And good job @peteriman, you're a standup guy and I appreciate you being very clear and verbose in your comments, keep that up! 👍 |
@thernstig Sure thing! Thanks for your affirmation, makes me feel what I do is worth the effort! 👍 |
@thernstig I have released it in |
This was first brought up here #788, and I've upgraded to the latest version and the problem still persists.
log4js does not properly write to the file, it seems to close the stream flushing out the latest input, as seen by
log4js.fileAppender - Writing to file /home/userA/code/projectA/log/projectA-server-config.log, error happened Error [ERR_STREAM_WRITE_AFTER_END]: write after end
.Code
Output
The text was updated successfully, but these errors were encountered: