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

observer.stop() creates file handle for watched directory #271

Closed
altendky opened this issue Sep 9, 2014 · 2 comments
Closed

observer.stop() creates file handle for watched directory #271

altendky opened this issue Sep 9, 2014 · 2 comments
Labels

Comments

@altendky
Copy link
Contributor

altendky commented Sep 9, 2014

I am working on a script which will wrap a closed source data editing program. I need my script to monitor the files generated by the editor so that I can post-process them. Since I am post-processing I have the editor operate in a tempfile.mkdtemp() directory and then try to delete the directory at the end. The monitoring works ok but I get an exception when I try to delete the temporary directory. There is no issue when I monitor the temp directory instead of the created sub-directory (I can use it this way with multiple temp directories as a work-around). Here's my attempt at a minimal recreation of the issue.

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import tempfile
import shutil
import os


class Handler(FileSystemEventHandler):
    def on_modified(self, event):
        print('Modified: {}'.format(event.src_path))

if __name__ == "__main__":
    temp_directory = tempfile.mkdtemp()
    print(temp_directory)
    sub_dir = os.path.join(temp_directory, 'subdir')
    os.makedirs(sub_dir)

    event_handler = Handler()
    observer = Observer()
    observer.schedule(event_handler, sub_dir, recursive=True)
    observer.start()

    observer.stop()
    observer.join()

    try:
        shutil.rmtree(temp_directory)
        print("No exception using shutil.rmtree().  Must not "
              "be exhibiting the reported issue.")
    except Exception as e:
        print("Exception occurred using shutil.rmtree().")
        print(e)
        print('\n'.join(os.listdir(temp_directory)))

While stepping through I was monitoring the active file handles for the process in Process Explorer and the directory name shows up when the call is made to the Windows function CancelIoEx() inside close_directory_handle() in winapi.py.

I have run this in both Python 3.3 and 3.4 with the same results. The OS is Windows7 x64 (with lots of annoying security software since it is my work laptop). I tried it on my coworkers computer with a similar setup and it, not surprisingly, had the same issue. PIP freeze provides:

PyYAML==3.11
argh==0.25.0
beautifulsoup4==4.3.2
cx-Freeze==4.3.3
ompython==2.0
pathtools==0.1.2
pyparsing==2.0.2
watchdog==0.8.1
@tamland
Copy link
Collaborator

tamland commented Sep 9, 2014

Like #270? I don't have a windows vm available atm so I can't test. Can you check if the patch fixes the issue?

@tamland tamland added the windows label Sep 9, 2014
@altendky
Copy link
Contributor Author

altendky commented Sep 9, 2014

Yep, that does it. Apparently I didn't search/read well enough when looking for a duplicate. Thanks for pointing this out.

@altendky altendky closed this as completed Sep 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants