I was aware that my shrinking-file behaviour wasn't well defined yet (except if/when watchit emits 'create' or 'unlink' events), but I agree that it is a bad race condition between the statSync() and createReadStream() calls. I didn't want to open and hold a file descriptor in case it messes up any "primary" (log writing) processes, but I'll have to be careful not to crash utterly. I want to learn how to write JavaScript that's a robust as possible, so I'd better start handling more exceptions (like, from calling createReadStream() on a nonexistent file... which will probably/certainly crash any application using it).
Thanks for your help. Adam On Feb 8, 6:08 pm, Matt <[email protected]> wrote: > There's a race between when you get the results from stat and re-open the > file. You know it's changed, you assume it has grown, so you re-open at > "last-byte-read" position. But what if between that time, the file got > truncated to zero, meaning you need to read from the start of the file? > > On Wed, Feb 8, 2012 at 4:36 PM, Adam Pritchard > <[email protected]>wrote: > > > > > > > > > Can you elaborate? I'm pretty new to JS/Node, so maybe I'm not seeing it. > > > I recognize that the file reading isn't one-to-one with the notifications > > (i.e., many rapid file modifications will probably be processed in a single > > file read, and then following notifications won't have an effect until the > > file grows again), but I don't see that that's a problem -- the data should > > still get processed in a timely fashion. > > > On the other hand... my tests aren't running through successfully on two > > of three OSes, so there's certainly something not right... > > > I'd appreciate any bugs or flaws you can point out. > > > Adam > > > On Wed, Feb 8, 2012 at 11:40 AM, Matt <[email protected]> wrote: > > >> Still has a race condition in it. > > >> On Tue, Feb 7, 2012 at 8:20 PM, Adam Pritchard > >> <[email protected]>wrote: > > >>> I've also been working on a little log-file-following (tail -f) node > >>> module. > >>>https://github.com/adam-p/text-file-follower/blob/master/lib/index.co... > > >>> I need it to work on Windows, so it's cross-platform. It works okay > >>> already, but I still have some stuff to do. > > >>> The behaviour of fs.watch is a bit sketchy. For example... > >>>https://github.com/joyent/node/issues/search?q=fs.watch&state=open > > >>> If I didn't need/want to support Windows I'd probably use fs.watchFile. > > >>> -- > >>> Job Board:http://jobs.nodejs.org/ > >>> Posting guidelines: > >>>https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > >>> You received this message because you are subscribed to the Google > >>> Groups "nodejs" group. > >>> To post to this group, send email to [email protected] > >>> To unsubscribe from this group, send email to > >>> [email protected] > >>> For more options, visit this group at > >>>http://groups.google.com/group/nodejs?hl=en?hl=en > > >> -- > >> Job Board:http://jobs.nodejs.org/ > >> Posting guidelines: > >>https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > >> You received this message because you are subscribed to the Google > >> Groups "nodejs" group. > >> To post to this group, send email to [email protected] > >> To unsubscribe from this group, send email to > >> [email protected] > >> For more options, visit this group at > >>http://groups.google.com/group/nodejs?hl=en?hl=en > > > -- > > Job Board:http://jobs.nodejs.org/ > > Posting guidelines: > >https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > > You received this message because you are subscribed to the Google > > Groups "nodejs" group. > > To post to this group, send email to [email protected] > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group at > >http://groups.google.com/group/nodejs?hl=en?hl=en -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
