Repository navigation
[staging CI] unslothai/unsloth#8170 #217
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
Changes from all commits
cb125b6
f7505ee
c6634d5
67c9c55
c1d0c3b
d2b4100
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| name: "staging-8170 macos-14" | ||
| on: | ||
| push: | ||
| branches: ["pr-8170-xplat-ci"] | ||
| paths: | ||
| - 'studio/backend/core/inference/llama_cpp.py' | ||
| - 'studio/backend/core/inference/tools.py' | ||
| - 'studio/backend/run.py' | ||
| - 'studio/backend/tests/test_child_lifetime_boundary.py' | ||
| - 'studio/backend/tests/test_orphaned_children.py' | ||
| - 'studio/backend/utils/process_lifetime.py' | ||
| - 'studio/frontend/src/hooks/use-tauri-update.ts' | ||
| - 'studio/src-tauri/src/desktop_updater.rs' | ||
| - 'studio/src-tauri/src/main.rs' | ||
| - 'studio/src-tauri/src/windows_job.rs' | ||
| - '.github/workflows/staging-8170-macos-14.yml' | ||
| workflow_dispatch: | ||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.ref }} | ||
| cancel-in-progress: true | ||
| permissions: | ||
| contents: read | ||
| defaults: | ||
| run: | ||
| shell: bash | ||
| jobs: | ||
| test: | ||
| runs-on: macos-14 | ||
| timeout-minutes: 30 | ||
| env: | ||
| UNSLOTH_COMPILE_DISABLE: '1' | ||
| UNSLOTH_IS_PRESENT: '1' | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.12' | ||
| - run: python -m pip install -r studio/backend/requirements/studio.txt pytest pytest-asyncio pytest-timeout python-multipart | ||
| - run: rc=0; PYTHONPATH=studio/backend python -m pytest studio/backend/tests/test_child_lifetime_boundary.py studio/backend/tests/test_orphaned_children.py -q --tb=short -k 'not llama_cpp_load_progress_live and not TestGpuAutoSelection and not TestPreSpawnGpuResolution and not TestPerGpuFitGuardAllCounts and not TestTransformersIntrospection and not test_returns_cuda_when_cuda_available and not test_calls_cuda_cache_when_cuda' || rc=$?; if [ "$rc" = "5" ]; then echo "no tests ran (deps absent on this runner)"; rc=0; fi; exit "$rc" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| name: "staging-8170 ubuntu-latest" | ||
| on: | ||
| push: | ||
| branches: ["pr-8170-xplat-ci"] | ||
| paths: | ||
| - 'studio/backend/core/inference/llama_cpp.py' | ||
| - 'studio/backend/core/inference/tools.py' | ||
| - 'studio/backend/run.py' | ||
| - 'studio/backend/tests/test_child_lifetime_boundary.py' | ||
| - 'studio/backend/tests/test_orphaned_children.py' | ||
| - 'studio/backend/utils/process_lifetime.py' | ||
| - 'studio/frontend/src/hooks/use-tauri-update.ts' | ||
| - 'studio/src-tauri/src/desktop_updater.rs' | ||
| - 'studio/src-tauri/src/main.rs' | ||
| - 'studio/src-tauri/src/windows_job.rs' | ||
| - '.github/workflows/staging-8170-ubuntu-latest.yml' | ||
| workflow_dispatch: | ||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.ref }} | ||
| cancel-in-progress: true | ||
| permissions: | ||
| contents: read | ||
| defaults: | ||
| run: | ||
| shell: bash | ||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 30 | ||
| env: | ||
| UNSLOTH_COMPILE_DISABLE: '1' | ||
| UNSLOTH_IS_PRESENT: '1' | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.12' | ||
| - run: python -m pip install -r studio/backend/requirements/studio.txt pytest pytest-asyncio pytest-timeout python-multipart | ||
| - run: rc=0; PYTHONPATH=studio/backend python -m pytest studio/backend/tests/test_child_lifetime_boundary.py studio/backend/tests/test_orphaned_children.py -q --tb=short -k 'not llama_cpp_load_progress_live and not TestGpuAutoSelection and not TestPreSpawnGpuResolution and not TestPerGpuFitGuardAllCounts and not TestTransformersIntrospection and not test_returns_cuda_when_cuda_available and not test_calls_cuda_cache_when_cuda' || rc=$?; if [ "$rc" = "5" ]; then echo "no tests ran (deps absent on this runner)"; rc=0; fi; exit "$rc" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| name: "staging-8170 windows-latest" | ||
| on: | ||
| push: | ||
| branches: ["pr-8170-xplat-ci"] | ||
| paths: | ||
| - 'studio/backend/core/inference/llama_cpp.py' | ||
| - 'studio/backend/core/inference/tools.py' | ||
| - 'studio/backend/run.py' | ||
| - 'studio/backend/tests/test_child_lifetime_boundary.py' | ||
| - 'studio/backend/tests/test_orphaned_children.py' | ||
| - 'studio/backend/utils/process_lifetime.py' | ||
| - 'studio/frontend/src/hooks/use-tauri-update.ts' | ||
| - 'studio/src-tauri/src/desktop_updater.rs' | ||
| - 'studio/src-tauri/src/main.rs' | ||
| - 'studio/src-tauri/src/windows_job.rs' | ||
| - '.github/workflows/staging-8170-windows-latest.yml' | ||
| workflow_dispatch: | ||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.ref }} | ||
| cancel-in-progress: true | ||
| permissions: | ||
| contents: read | ||
| defaults: | ||
| run: | ||
| shell: bash | ||
| jobs: | ||
| test: | ||
| runs-on: windows-latest | ||
| timeout-minutes: 30 | ||
| env: | ||
| UNSLOTH_COMPILE_DISABLE: '1' | ||
| UNSLOTH_IS_PRESENT: '1' | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.12' | ||
| - run: python -m pip install -r studio/backend/requirements/studio.txt pytest pytest-asyncio pytest-timeout python-multipart | ||
| - run: rc=0; PYTHONPATH=studio/backend python -m pytest studio/backend/tests/test_child_lifetime_boundary.py studio/backend/tests/test_orphaned_children.py -q --tb=short -k 'not llama_cpp_load_progress_live and not TestGpuAutoSelection and not TestPreSpawnGpuResolution and not TestPerGpuFitGuardAllCounts and not TestTransformersIntrospection and not test_returns_cuda_when_cuda_available and not test_calls_cuda_cache_when_cuda' || rc=$?; if [ "$rc" = "5" ]; then echo "no tests ran (deps absent on this runner)"; rc=0; fi; exit "$rc" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1136,6 +1136,73 @@ def _remove_pid_file(): | |
| pass | ||
|
|
||
|
|
||
| # Windows terminates the process ~5s after a close event, so leave a margin. | ||
| _CONSOLE_SHUTDOWN_BUDGET = 4.5 | ||
|
|
||
|
|
||
| def _run_console_shutdown(shutdown) -> None: | ||
| try: | ||
| shutdown() | ||
| except Exception as error: | ||
| logger.warning("Console-close cleanup failed: %s", error) | ||
|
|
||
|
|
||
| def _install_windows_console_handler(shutdown) -> bool: | ||
| """Run the graceful shutdown when the console window is closed. | ||
|
|
||
| Closing the window raises CTRL_CLOSE_EVENT, which Python never turns into a | ||
| signal, so neither a signal handler nor atexit runs and cleanup is skipped. | ||
| ``shutdown`` takes no arguments and must not touch signal.signal: Windows | ||
| runs this on a thread it creates for the event, and Windows kills the | ||
| process about five seconds later, so the work is bounded to fit. No-op off | ||
| Windows. | ||
| """ | ||
| if sys.platform != "win32": | ||
| return False | ||
| try: | ||
| import ctypes | ||
| from ctypes import wintypes | ||
|
|
||
| CTRL_C_EVENT, CTRL_BREAK_EVENT = 0, 1 | ||
| CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT = 2, 5, 6 | ||
| HANDLER = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD) | ||
|
|
||
| import threading | ||
|
|
||
| def _on_console_event(event: int) -> bool: | ||
| if event in (CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT): | ||
| worker = threading.Thread( | ||
| target = _run_console_shutdown, args = (shutdown,), daemon = True | ||
| ) | ||
| worker.start() | ||
| worker.join(timeout = _CONSOLE_SHUTDOWN_BUDGET) | ||
| return True | ||
| # Ctrl+C / Ctrl+Break already arrive as Python signals; pass them | ||
| # on rather than shutting down twice. | ||
| del CTRL_C_EVENT, CTRL_BREAK_EVENT | ||
| return False | ||
|
|
||
| callback = HANDLER(_on_console_event) | ||
| kernel32 = ctypes.WinDLL("kernel32", use_last_error = True) | ||
| kernel32.SetConsoleCtrlHandler.argtypes = [HANDLER, wintypes.BOOL] | ||
| kernel32.SetConsoleCtrlHandler.restype = wintypes.BOOL | ||
| if not kernel32.SetConsoleCtrlHandler(callback, True): | ||
| logger.warning( | ||
| "Could not install the console-close handler (WinError %s); closing the " | ||
| "window will skip subprocess cleanup.", | ||
| ctypes.get_last_error(), | ||
| ) | ||
| return False | ||
| # Hold a reference: a collected callback leaves Windows calling into | ||
| # freed memory. | ||
| globals()["_WINDOWS_CONSOLE_HANDLER"] = callback | ||
| logger.info("Console-close handler installed") | ||
| return True | ||
| except Exception as error: | ||
| logger.warning("Could not install the console-close handler: %s", error) | ||
| return False | ||
|
|
||
|
|
||
| def _graceful_shutdown(server = None): | ||
| """Shut down all subprocess backends and the uvicorn server. | ||
|
|
||
|
|
@@ -1189,8 +1256,9 @@ def _graceful_shutdown(server = None): | |
|
|
||
| # 7. Backstop sweep for any adopted child the steps above missed. | ||
| try: | ||
| from utils.process_lifetime import terminate_all | ||
| from utils.process_lifetime import clear_breadcrumb, terminate_all | ||
| terminate_all() | ||
| clear_breadcrumb() # nothing left for the next startup to sweep | ||
| except Exception as e: | ||
| logger.warning("Error in process-lifetime sweep: %s", e) | ||
|
|
||
|
|
@@ -1849,9 +1917,18 @@ def run_server( | |
|
|
||
| # Reap every child if the parent dies abnormally (terminal close, Task | ||
| # Manager kill, SIGKILL); must run before any child can spawn. | ||
| from utils.process_lifetime import initialize_parent_lifetime | ||
| from utils.process_lifetime import initialize_parent_lifetime, reap_recorded_children | ||
|
|
||
| initialize_parent_lifetime() | ||
| # macOS has neither PR_SET_PDEATHSIG nor job objects, so a Studio that | ||
| # crashed left its sidecars running. Sweep before spawning anything: a | ||
| # leftover holds VRAM, a port, and the files an update has to replace. | ||
| try: | ||
| reaped = reap_recorded_children() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Studio is killed on macOS during a Python or shell tool call, the child continues running because Useful? React with 👍 / 👎. |
||
| if reaped: | ||
| logger.warning("Reaped %d orphan(s) from a previous Studio: %s", len(reaped), reaped) | ||
| except Exception as e: | ||
| logger.warning("Could not sweep orphans from a previous run: %s", e) | ||
|
|
||
| # --secure exposes ONLY the Cloudflare link: reject --secure --no-cloudflare, | ||
| # then force a loopback bind so the raw port is never public (even -H 0.0.0.0). | ||
|
|
@@ -2488,6 +2565,15 @@ def _signal_handler(signum, frame): | |
| if hasattr(signal, "SIGBREAK"): | ||
| signal.signal(signal.SIGBREAK, _signal_handler) | ||
|
|
||
| # NOT _signal_handler: Windows runs this on a thread it creates, and | ||
| # signal.signal() off the main thread raises, which would leave the window | ||
| # close doing no cleanup at all. | ||
| def _console_shutdown(): | ||
| _graceful_shutdown(_server) | ||
| _shutdown_event.set() | ||
|
|
||
| _install_windows_console_handler(_console_shutdown) | ||
|
|
||
| # Keep running until shutdown signal. Event.wait() without a timeout blocks at | ||
| # the C level on Linux, preventing SIGINT delivery; a short timeout in a loop | ||
| # lets the interpreter process pending signals. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Windows, Ctrl+C and Ctrl+Break take this non-close branch, but
del CTRL_C_EVENT, CTRL_BREAK_EVENTmakes both names local to the callback and raisesUnboundLocalErrorbeforereturn False. Because this runs through a ctypes console-control callback, the exception is reported/ignored at the callback boundary instead of intentionally passing the event onward to Python's signal handler, making Ctrl-event handling unreliable.Useful? React with 👍 / 👎.