Skip to content

Commit 357a0c8

Browse files
committed
Enable graceful termination on Windows
1 parent 850ba9c commit 357a0c8

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/lmstudio/plugin/cli.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,25 @@ def main(argv: Sequence[str] | None = None) -> int:
4141
parser.print_usage()
4242
print(f"ERROR: Failed to find plugin folder at {plugin_path!r}")
4343
return 1
44+
# TODO: Leave the warning enabled in the dev plugin wrapper
4445
warnings.filterwarnings(
4546
"ignore", ".*the plugin API is not yet stable", FutureWarning
4647
)
4748
log_level = logging.DEBUG if args.debug else logging.INFO
4849
logging.basicConfig(level=log_level)
50+
if sys.platform == "win32":
51+
# Accept Ctrl-C events even in non-default process groups
52+
# (allows for graceful termination when Ctrl-C is received
53+
# from a controlling process rather than from a console)
54+
# Based on https://github.com/python/cpython/blob/3.14/Lib/test/win_console_handler.py
55+
# and https://stackoverflow.com/questions/35772001/how-can-i-handle-a-signal-sigint-on-a-windows-os-machine/35792192#35792192
56+
from ctypes import c_void_p, windll, wintypes
57+
58+
SetConsoleCtrlHandler = windll.kernel32.SetConsoleCtrlHandler
59+
SetConsoleCtrlHandler.argtypes = (c_void_p, wintypes.BOOL)
60+
SetConsoleCtrlHandler.restype = wintypes.BOOL
61+
if not SetConsoleCtrlHandler(None, 0):
62+
print("Failed to enable Ctrl-C events, termination may be abrupt")
4963
if not args.dev:
5064
try:
5165
runner.run_plugin(plugin_path, allow_local_imports=True)

0 commit comments

Comments
 (0)