Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions pyrit/ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import subprocess
import sys
import traceback
import ctypes
import ctypes

GLOBAL_MUTEX_NAME = "PyRIT-Gradio"

Expand All @@ -25,15 +27,18 @@ def launch_app(open_browser: bool = False) -> None:
subprocess.Popen([python_path, current_path, str(open_browser)])


def is_app_running() -> bool:
def create_mutex() -> bool:
if sys.platform != "win32":
raise NotImplementedError("This function is only supported on Windows.")
return True
last_error = ctypes.windll.kernel32.GetLastError()
return bool(last_error != 183) # ERROR_ALREADY_EXISTS


import ctypes.wintypes # noqa: F401
def is_app_running() -> bool:
if sys.platform != "win32":
return False

SYNCHRONIZE = 0x00100000
mutex = ctypes.windll.kernel32.OpenMutexW(SYNCHRONIZE, False, GLOBAL_MUTEX_NAME)
if not mutex:
return False

# Close the handle to the mutex
Expand All @@ -43,17 +48,6 @@ def is_app_running() -> bool:

if __name__ == "__main__":

def create_mutex() -> bool:
if sys.platform != "win32":
raise NotImplementedError("This function is only supported on Windows.")

# TODO make sure to add cross-platform support for this.
import ctypes.wintypes

ctypes.windll.kernel32.CreateMutexW(None, False, GLOBAL_MUTEX_NAME)
last_error = ctypes.windll.kernel32.GetLastError()
return bool(last_error != 183) # ERROR_ALREADY_EXISTS

if not create_mutex():
print("Gradio UI is already running.")
sys.exit(1)
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/ui/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

from pyrit.ui import app as ui_app


def test_is_app_running_returns_false_on_non_windows(monkeypatch):
monkeypatch.setattr(ui_app.sys, "platform", "linux")

assert ui_app.is_app_running() is False


def test_create_mutex_returns_true_on_non_windows(monkeypatch):
monkeypatch.setattr(ui_app.sys, "platform", "linux")

assert ui_app.create_mutex() is True
Loading