[PR #106] [MERGED] Refuse a second init_devices() before plat_init() #119

Closed
opened 2026-10-26 22:24:44 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Comfy-Org/comfy-aimdo/pull/106
Author: @PozzettiAndrea
Created: 9/5/2026
Status: ✅ Merged
Merged: 9/6/2026
Merged by: @rattus128

Base: master ← Head: pr/init-devices-guard


📝 Commits (1)

  • 77b5882 Refuse a second init_devices() before plat_init()

📊 Changes

2 files changed (+23 additions, -0 deletions)

View changed files

📝 comfy_aimdo/control.py (+4 -0)
➕ tests/init-devices-twice-cuda.py (+19 -0)

📄 Description

Calling init_devices() twice crashes the process on the next allocation.

The second call runs plat_init() again. That installs a new set of CUDA hooks over the live ones, so the first set is orphaned. lib.init() then rejects the duplicate devices, and the failure path calls plat_cleanup(), which tears down the live hooks and zeroes the dispatch table. The next torch allocation lands in the orphaned hook and calls cuCtxGetDevice through a NULL slot (src/control.c:102). Python saw init_devices() return False and then died.

Refuse the call while devctxs is non-empty. deinit() first is the supported way to reinitialize, as before. Python only; plat_init() itself stays non-reentrant (aimdo_setup_hooks stacks a second hook set, xfer_file_init resets a live thread pool), which is a separate C change I can send next.

Found while probing from comfy-env, which initializes aimdo per worker subprocess (memory_manager.py:261-268). tests/init-devices-twice-cuda.py: exit 139 on 0.4.13, passes with this patch. RTX 3090, torch 2.8 cu128.

Contribution Agreement

  • I agree that my contributions are licensed under the GPLv3.
  • I grant Comfy Org the rights to relicense these contributions as outlined in CONTRIBUTING.md.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Comfy-Org/comfy-aimdo/pull/106 **Author:** [@PozzettiAndrea](https://github.com/PozzettiAndrea) **Created:** 9/5/2026 **Status:** ✅ Merged **Merged:** 9/6/2026 **Merged by:** [@rattus128](https://github.com/rattus128) **Base:** `master` ← **Head:** `pr/init-devices-guard` --- ### 📝 Commits (1) - [`77b5882`](https://github.com/Comfy-Org/comfy-aimdo/commit/77b588231682cfa2d0777fa97da3e6081dda6408) Refuse a second init_devices() before plat_init() ### 📊 Changes **2 files changed** (+23 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `comfy_aimdo/control.py` (+4 -0) ➕ `tests/init-devices-twice-cuda.py` (+19 -0) </details> ### 📄 Description Calling `init_devices()` twice crashes the process on the next allocation. The second call runs `plat_init()` again. That installs a new set of CUDA hooks over the live ones, so the first set is orphaned. `lib.init()` then rejects the duplicate devices, and the failure path calls `plat_cleanup()`, which tears down the live hooks and zeroes the dispatch table. The next torch allocation lands in the orphaned hook and calls `cuCtxGetDevice` through a NULL slot (`src/control.c:102`). Python saw `init_devices()` return False and then died. Refuse the call while `devctxs` is non-empty. `deinit()` first is the supported way to reinitialize, as before. Python only; `plat_init()` itself stays non-reentrant (`aimdo_setup_hooks` stacks a second hook set, `xfer_file_init` resets a live thread pool), which is a separate C change I can send next. Found while probing from [comfy-env](https://github.com/PozzettiAndrea/comfy-env), which initializes aimdo per worker subprocess ([memory_manager.py:261-268](https://github.com/PozzettiAndrea/comfy-env/blob/7f61cc62f1ea62cd4ab7169a38bf055556eedf2c/src/comfy_env/memory_manager.py#L261-L268)). `tests/init-devices-twice-cuda.py`: exit 139 on 0.4.13, passes with this patch. RTX 3090, torch 2.8 cu128. ### Contribution Agreement - [x] I agree that my contributions are licensed under the GPLv3. - [x] I grant **Comfy Org** the rights to relicense these contributions as outlined in [CONTRIBUTING.md](./CONTRIBUTING.md). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-26 22:24:44 +00:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Comfy-Org/comfy-aimdo#119
No description provided.