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

Fix race condition and potential error in logfile deleting #159

Open
Digenis opened this issue Jul 21, 2016 · 0 comments
Open

Fix race condition and potential error in logfile deleting #159

Digenis opened this issue Jul 21, 2016 · 0 comments

Comments

@Digenis
Copy link
Member

Digenis commented Jul 21, 2016

This is a bit of a [WIP] issue because there are more details to be added when I have the time

environ.py has the code responsible for deleting logfiles.

    def _get_file(self, message, dir, ext):
        logsdir = os.path.join(dir, message['_project'], \
            message['_spider'])
        if not os.path.exists(logsdir):
            os.makedirs(logsdir)
        to_delete = sorted((os.path.join(logsdir, x) for x in \
            os.listdir(logsdir)), key=os.path.getmtime)[:-self.jobs_to_keep]
        for x in to_delete:
            os.remove(x)
        return os.path.join(logsdir, "%s.%s" % (message['_job'], ext))

This deletes logs of hanged spiders.
When a spider hangs, it stops updating its log's mtime
and its log can be soon deleted by this code.
On windows, it should even crash because it doesn't allow open files to be deleted.

psutil can be used to check if the file is still open.
It is already being discussed as an optional dependency in scrapy (scrapy/scrapy#1329)

There is also a race condition between to_delete = sorted(...) and os.remove().
If something else removes the file, it will break. ENOENT should be ignored.

@jpmckinney jpmckinney changed the title Logfile deleting Fix race condition and potential error in logfile deleting May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant