gorakhargosh / watchdog

Python library and shell utilities to monitor filesystem events.
http://packages.python.org/watchdog/
Apache License 2.0
6.53k stars 695 forks source link

Is it possible to prevent being connected to named pipe on Windows platform? #1038

Open nagok opened 6 months ago

nagok commented 6 months ago

On Windows platform '\\.\pipe\' directory can be monitored to catch the creation of named pipes.

When the following program is executed, the connection to the named pipe should not be processed, but the connection is made at the point of creation of the named pipe on the server process side, resulting in an error within the waiting process of the server process.

class PipeCreateEventHandler(FileSystemEventHandler):
    def on_created(self, event):
        print(f"on_created: {event.src_path}")
        if event.src_path == r'\\.\pipe\mypipe':
            observer.stop()

observer = Observer()
observer.schedule(PipeCreateEventHandler(), r'\\.\pipe''\\', recursive=False, event_filter=[FileCreatedEvent])
observer.start()
observer.join()

# Do something using '\\.\pipe\mypipe'

Perhaps the os.path.isdir() call in read_directory_changes.py is the cause.

                elif winapi_event.is_added:
                    isdir = os.path.isdir(src_path)

For reference, here is a server program for testing with PowerShell.

echo "Create mypipe"
$pipe = New-Object System.IO.Pipes.NamedPipeServerStream "mypipe", InOut

echo "WaitForConnection"
$pipe.WaitForConnection()    # MethodInvocationException raised

echo "Connect"

# Do something using '\\.\pipe\mypipe'