Skip to content

bugfix: Avoid removing modules added by Reactor from sys.modules.#194

Open
guyniv wants to merge 1 commit intoGourieff:mainfrom
guyniv:feature/fix-multiple-torchao-imports-upstream
Open

bugfix: Avoid removing modules added by Reactor from sys.modules.#194
guyniv wants to merge 1 commit intoGourieff:mainfrom
guyniv:feature/fix-multiple-torchao-imports-upstream

Conversation

@guyniv
Copy link

@guyniv guyniv commented Oct 30, 2025

Intro:

Usually when multiple files try to import a module, the first time will trigger an import, and the imported module will be cached in sys.modules. The second time an import happens it will get the module from sys.modules.

Why:

It appears that in __init__.py we remove any new module that was added by Reactor, apart from a specific white list.
The meaning of this is that if a module has a side effect when we import it, and the same module will be imported in another place, python won't find it in sys.modules, and the side effect will happen again.

To be specific, it causes an issue with torchao, which its import triggers the following line:

lib.define("int_matmul(Tensor a, Tensor b) -> Tensor")

Which will throw this error when it's imported the second time:

RuntimeError: Tried to register an operator (torchao::int_matmul(Tensor a, Tensor b) -> Tensor) with the same name and overload name multiple times.

How to Reproduce:

To trigger a second import to torchao, add this line to the end of __init__.py:

import torchao

@guyniv
Copy link
Author

guyniv commented Oct 30, 2025

This PR solves #164

Usually when multiple files try to import a module, the first time will
trigger an import, and the imported module will be cached in
`sys.modules`. The second time an import happens it will get the module
from `sys.module`.

It appears that in `__init__.py` we remove any new module that was added
by Reactor, apart from a specific white list.
The meaning of this is that if a module has a side effect when we import
it, if the same module will be imported it won't find it in
`sys.module`, and the side effect will happen again.

To be specific, it causes an issue with `torchao`, which its import triggers the following line:
```
lib.define("int_matmul(Tensor a, Tensor b) -> Tensor")
```

Which will throw this error when it's execute twice:
```
RuntimeError: Tried to register an operator (torchao::int_matmul(Tensor a, Tensor b) -> Tensor) with the same name and overload name multiple times.
```
@guyniv guyniv force-pushed the feature/fix-multiple-torchao-imports-upstream branch from ab10d1c to cb992b0 Compare October 30, 2025 10:22
ChrisFab16 added a commit to ChrisFab16/ComfyUI-ReActor that referenced this pull request Feb 9, 2026
The cleanup code added in PR Gourieff#194 removes modules from sys.modules
that other custom nodes depend on, causing segmentation faults.

This fix removes the aggressive module cleanup while keeping
path cleanup and a1111 webui module restoration.

Also unpins versions for flexibility:
- insightface>=0.7.3
- numpy>=1.26.4
@Gourieff
Copy link
Owner

Gourieff commented Feb 12, 2026

Thanks for PR my friend
It was a fix to make ReActor workable in ComfyUI via A1111
The code will be refactored soon (I hope it will be soon 🤞), so I will clean up all the crutches like that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants