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

log4js version 2.10.0 is giving error when shutdown and process Exit called #788

Closed
DJMit-alt opened this issue Sep 25, 2018 · 10 comments · Fixed by #1113
Closed

log4js version 2.10.0 is giving error when shutdown and process Exit called #788

DJMit-alt opened this issue Sep 25, 2018 · 10 comments · Fixed by #1113

Comments

@DJMit-alt
Copy link

code:
log4js.shutdown(() => { process.exit(1); });

Error:
log4js.fileAppender - Writing to file ../logs/ABC.log, error happened Error: write after end
at writeAfterEnd (/node_modules/readable-stream/lib/_stream_writable.js:288:12)
at RollingFileStream.Writable.write (/node_modules/readable-stream/lib/_stream_writable.js:332:20)
at Function.app.shutdown (node_modules/log4js/lib/appenders/file.js:69:12)
at appenders.filter.forEach.a (/node_modules/log4js/lib/log4js.js:273:52)
at Array.forEach (native)
at Object.shutdown (/node_modules/log4js/lib/log4js.js:273:37)
at process.exitHandler (XYZ.js:130:12)
at emitOne (events.js:96:13)
at process.emit (events.js:188:7)
at process.exit (internal/process.js:146:15)

@nomiddlename
Copy link
Collaborator

This issue might give you more information: #542 - the problem is that process.exit happens synchronously, before the appenders have finished their final writes. You could try adding a setTimeout call around the process.exit and waiting a second or two?

@DJMit-alt
Copy link
Author

DJMit-alt commented Oct 4, 2018

I am calling process.exit asynchronously in shutdown callback. It not a synchronous request and whatever the scenario is , it should not give exception. right??

@nomiddlename
Copy link
Collaborator

Closing this one, because as mentioned in #542 there's not a lot we can do about it.

@thernstig
Copy link

@nomiddlename Could you elaborate this a bit more. In #542 the initial post says:

has the problem that process.exit is called asynchronously.

But according to https://nodejs.org/api/process.html#process_process_exit_code it is called synchronously. This makes that issue harder to understand, because one cannot be sure you are talking the same lingo.

Is the real problem this?

When the callback of log4js.shutdown() is called, after it has flushed it streams, calling process.exit() will be done synchronously and thus no other pending asynchronous operations should be able to occur. So how can this occur?

@nomiddlename
Copy link
Collaborator

Yep, you're right - process.exit is synchronous and terminates any pending asynchronous operations.

@thernstig
Copy link

thernstig commented Oct 30, 2019

@nomiddlename Then how is it that the original poster's concern is not valid?

If the callback of log4js.shutdown() is called, it means that callback is currently running on the nodejs thread. If a call to process.exit() then is done synchronously, how can the log4js.fileAppender in any way continue writing so that this occurs: log4js.fileAppender - Writing to file ../logs/ABC.log, error happened Error: write after end ?

And to follow up that question. If log4js.shutdown() cannot handle this problem, is there any sense in implementing a call to that function as the documentation suggests?

@nomiddlename
Copy link
Collaborator

Is this a problem you're encountering with the latest version of log4js? If so, have you got some code that reproduces the issue? I can then see if we can fix this or not.

@thernstig
Copy link

@nomiddlename Let me upgrade my version first to see if this is a problem. I just read into your previous answer that this is not solvable, but if you are saying that the latest version should not have this problem then I will definitely try an upgrade and see how it goes. It might take a while, but I will get back to you once I get the time, but I will upgrade!

@ayavilevich
Copy link

If you are having this issue, make sure you are not running log4js.shutdown more than once in parallel. I had such errors when I was running log4js.shutdown several times before it has time to complete the async handler.

@lamweili
Copy link
Contributor

#1113 fixed this issue in log4js@6.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants