rotating-file-stream icon indicating copy to clipboard operation
rotating-file-stream copied to clipboard

[bug] Cannot read properties of undefined (reading 'write')

Open robertsLando opened this issue 1 year ago • 4 comments

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?

robertsLando avatar Jul 12 '22 06:07 robertsLando

Sorry for my late reply.

No! Any clue 😞 Is that the entire stack trace or is there something else? Thank you

iccicci avatar Jul 25 '22 19:07 iccicci

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

robertsLando avatar Jul 26 '22 06:07 robertsLando

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.

iccicci avatar Jul 26 '22 09:07 iccicci

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

robertsLando avatar Jul 26 '22 11:07 robertsLando

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)

lonelywolf4git avatar Mar 20 '23 03:03 lonelywolf4git

I got same one when spinning while writing hundreds of lines per second.

Is there any workaround?

nuchiyama avatar May 25 '23 12:05 nuchiyama

Hi all,

given the high number or requests, I'll try to handle this some how in the next days.

iccicci avatar May 25 '23 17:05 iccicci

Awesome, thanks!

robertsLando avatar May 26 '23 06:05 robertsLando

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

iccicci avatar Jun 08 '23 21:06 iccicci

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.

robertsLando avatar Jun 09 '23 13:06 robertsLando

One more attempt @robertsLando and @nuchiyama ; could you please share me your RFS configuration? It could be it gives me some idea...

iccicci avatar Jun 10 '23 09:06 iccicci

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?

robertsLando avatar Jun 12 '23 07:06 robertsLando

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.

iccicci avatar Jun 12 '23 11:06 iccicci

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 )

nuchiyama avatar Jun 13 '23 01:06 nuchiyama

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

iccicci avatar Jun 13 '23 08:06 iccicci

Hi @robertsLando , could it be you are using cluster module as well?

Thank you

iccicci avatar Jun 13 '23 08:06 iccicci

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

robertsLando avatar Jun 13 '23 14:06 robertsLando

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

iccicci avatar Jun 13 '23 18:06 iccicci

Yeah I can, anyway I'm not using that application right now, If the bug shows up again I will come back to you

robertsLando avatar Jun 14 '23 15:06 robertsLando

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);

nuchiyama avatar Jun 16 '23 07:06 nuchiyama

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 avatar Jun 16 '23 12:06 iccicci

@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

robertsLando avatar Jun 19 '23 07:06 robertsLando

Maybe same as #95 ?

diwic avatar Jul 21 '23 09:07 diwic

Thanks to @diwic , v3.1.1 should solve this.

iccicci avatar Jul 22 '23 14:07 iccicci

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";

okoetter avatar Dec 13 '23 08:12 okoetter