rotating-file-stream
rotating-file-stream copied to clipboard
[bug] Cannot read properties of undefined (reading 'write')
I got this error
/app/packages/shared/node_modules/rotating-file-stream/dist/cjs/index.js:89
await this.file.write(chunk);
^
TypeError: Cannot read properties of undefined (reading 'write')
at RotatingFileStream.rewrite (/app/packages/shared/node_modules/rotating-file-stream/dist/cjs/index.js:89:33)
Running the application in a docker container, everything was running fine for around a week then this error caused the app to crash.
Any clue what could be the reason?
Sorry for my late reply.
No! Any clue 😞 Is that the entire stack trace or is there something else? Thank you
Ciao Daniele
Is that the entire stack trace or is there something else?
Nope I'm sorry I tried to reproduce it without success, I have a feel it's something related to docker and how it maps volumes in containers, seems like it didn't find the file
Much better @robertsLando , at least for me... I would have nothing to fix in that case 😉!
Apart from jokes, I propose to keep the issue opened, if you are able to reproduce once again the error, please report the entire stack trace; if you find the evidence that the problem was actually caused by something related to docker, please just add some more feedback and close the issue.
Giving that the error is hard to reproduce I was wondering if it could make sense to simply handle such case better, I mean the case when this.file
is undefined/null in rewrite
function. We could handle this by simply calling reopen
when it happens?
The check should be done here: https://github.com/iccicci/rotating-file-stream/blob/master/index.ts#L207
I also encountered this issue, not in docker . the stack trace is : TypeError: Cannot read property 'write' of undefined 6|storer | at RotatingFileStream.rewrite (/mnt/ssd/dacent/device-data-storer/node_modules/rotating-file-stream/dist/cjs/index.js:90:33)
I got same one when spinning while writing hundreds of lines per second.
Is there any workaround?
Hi all,
given the high number or requests, I'll try to handle this some how in the next days.
Awesome, thanks!
I'm sorry, I spent some hours but I wasn't able to find any valid action to take: standard rotation, calssical rotation and immutable rotation work differently each other, simply calling reopen
may be source of other problems.
The worst thing is that I can't reproduce the problem, I use RFS in some projects and I never suffered the same problem. I know that... https://img.memegenerator.net/instances/65290498.jpg ... but it's really hard to find a solution 😞
In order to support me, could I ask to share your configuration and try to log all the events fired by the stream?
Looking at the configuration and the events fired right before the error, I can try to identify its source. Thank you
Ciao Daniele,
thanks for looking at this, unfortunately the issue happens occasionally and I have no way to reproduce it, If I get a change to send you some useful logs I will do.
One more attempt @robertsLando and @nuchiyama ; could you please share me your RFS configuration? It could be it gives me some idea...
Could you explain me where I should find it?
Also just wondering, could it be this happens because 2 process are writing on same file for some reason?
Hi @robertsLando , when I speak about the configuration I mean the parameters you use to call createStream
.
And yes, having simultanously 2 open write stream on the same file is always a bad idea.
Hi, my configuration is { interval : '1m', maxFiles : 3 }
.
My program use cluster module ( 1child process ), the data that write to file get 1000 data per second via RabbitMQ.
- Node.js version: v16.18.1 ( OS : rocky linux 9 )
Hi @nuchiyama ,
My program use cluster module
This is very interesting! Are you using a fixed filename or a filname generator? Ref: filename
Could you please try to add the pid in the filename to check if this is the cause?
In case you are using a filname generator, you should add process.pid
in the filename.
In case you are using a fixed filename you can do something similar to:
const pad = num => (num > 9 ? "" : "0") + num;
const generator = (time, index) => {
if (!time) return "file.log";
var month = time.getFullYear() + "" + pad(time.getMonth() + 1);
var day = pad(time.getDate());
var hour = pad(time.getHours());
var minute = pad(time.getMinutes());
return `${month}${day}-${hour}${minute}-${index}-${process.pid}-file.log`;
};
const rfs = require("rotating-file-stream");
const stream = rfs.createStream(generator, { interval : '1m', maxFiles : 3 });
I admit I never thought to cluster module, hopefully you found the source of the problem 🤞
Please let me know! Thank you
Hi @robertsLando , could it be you are using cluster module as well?
Thank you
My configuration is:
const rotateStream = createStream(this.filename.bind(this), {
size: '3M', // max file size
maxSize: '200M', // max total size
maxFiles: 30, // keep up to 15 log files
path: logsPath, // base logs path
mode: 0o777, // file mode
});
Filename function:
function filename(now?: Date, index?: number): string {
let tag = '';
if (now) {
tag = `${now.getFullYear()}-${this.pad(now.getMonth() + 1)}-${this.pad(
now.getDate(),
)}.${index}`;
}
const filename = `${FileLogger.APP}.${this._channel}${
tag ? `.${tag}` : ''
}.log`;
return filename;
}
Nope I'm not using cluster module but there could be multiple processes writing on same file that I think leads to the same problem
Thank you @robertsLando , could you please try to write logs in distict files?
By adding ${process.pid}
in your filename generator or using distinct FileLogger.APP
or this._channel
on distict programs...
Thank you
Yeah I can, anyway I'm not using that application right now, If the bug shows up again I will come back to you
Thank you for reply. I got same error.
My source code summary:
import df from 'dateformat';
import rfs from 'rotating-file-stream';
if ( cluster.isPrimary ) {
cluster.fork();
} else {
const generator = ( time, index ) => {
if ( ! time ) return '/var/log/logging_' + process.pid;
return '/var/log/logging_' + process.pid + '-' + df( time, 'yyyymmddHHMM' ) + '-' + index;
};
const stream = rfs.createStream( generator, { interval : '1m', maxFiles : 3 } );
}
I tried change code, "rotating-file-stream/dist/cjs/index.js" ( around line 80 ), I never get error, but probebry lost data....
this.size += chunk.length;
if ( ! this.file ) { continue; } // <-Add code
await this.file.write(chunk);
Interesting @nuchiyama ! Your exceptional easy workaround change given me some ideas...
One more request, when it's possible:
I got same error.
in case of any error, sharing them could give me some more idea.
Thank you
@iccicci Giving that reproducing this is quite difficult I think the best would be to write a test that does this (close the file stream while writing) and then see if there could be an easy way to recover without break something else
Maybe same as #95 ?
Thanks to @diwic , v3.1.1 should solve this.
I still have this problem with v3.1.1. I'm using Typescript with these options in tsconfig.json:
{
"compilerOptions": {
"target": "es2016",
"module": "CommonJS",
"esModuleInterop": true
}
}
My server.ts:
import rfs from "rotating-file-stream";
const accessLogStream = rfs.createStream("access.log", {
interval: "1d"
});
When I use require
instead of import
then it works fine:
const rfs = require("rotating-file-stream");
Am I doing anything wrong here?
EDIT:
Nevermind, this works fine:
import * as rfs from "rotating-file-stream";