Repository navigation
Description
Given the following program:
from zimscraperlib.zim import Creator
from pathlib import Path
from zimscraperlib.zim import metadata
creator = Creator(Path("tests.zim"), "index.html").config_metadata(
std_metadata=metadata.DEFAULT_DEV_ZIM_METADATA
)
creator.start()
creator.add_item_for("index.html", "Main Page", content="Main", is_front=True )
for i in range(10):
creator.add_item_for(f"content{i}", content="P" * 1000000)
creator.add_item_for("conflict", content="Foo")
creator.add_item_for("conflict", content="Bar")I get following logs:
Traceback (most recent call last):
File "/home/benoit/Tools/zimcreateconflict.py", line 17, in <module>
creator.add_item_for("conflict", content="Bar")
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<@beartype(zimscraperlib.zim.creator.Creator.add_item_for) at 0x7b7c6f51f5e0>", line 248, in add_item_for
File "/home/benoit/Tools/venv3.14/lib/python3.14/site-packages/zimscraperlib/zim/creator.py", line 365, in add_item_for
self.add_item(
~~~~~~~~~~~~~^
StaticItem(
^^^^^^^^^^^
...<10 lines>...
duplicate_ok=duplicate_ok,
^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "<@beartype(zimscraperlib.zim.creator.Creator.add_item) at 0x7b7c6f51f740>", line 83, in add_item
File "/home/benoit/Tools/venv3.14/lib/python3.14/site-packages/zimscraperlib/zim/creator.py", line 410, in add_item
raise exc
File "/home/benoit/Tools/venv3.14/lib/python3.14/site-packages/zimscraperlib/zim/creator.py", line 407, in add_item
super().add_item(item)
~~~~~~~~~~~~~~~~^^^^^^
File "libzim/libzim.pyx", line 517, in libzim._Creator.add_item
RuntimeError: Impossible to add C/conflict
dirent's title to add is : conflict
existing dirent's title is : conflict
Which is normal. Scraper should not be doing this kind of silly thing.
What is abnormal is that the program hangs forever, waiting for something to complete. This should not happen (like the program should stop almost immediately).
I don't know it this is a pure libzim issue problem or something related to python-libzim (or python-scraperlib) so I'm asking for help to have confirmation regarding if you achieve to reproduce the problem in pure C++.
What seems to be key to reproduce the issue is to fill at least one cluster ; the more cluster we fill the hardest it is stop stop (with only range(10) in code above, python is still responding to Ctrl-C input. With range(100), I had to kill the program. With Without this initial content filling a cluster, everything works as expected (program terminates).
This has been discovered in openzim/maps#110
Activity
[-]Program hangs forever when add two items at same path[/-][+]Program hangs forever when adding two items at same path[/+]benoit74 commentedon Apr 28, 2026
Edit: fixed wrong word in last paragraph
veloman-yunkan commentedon Apr 28, 2026
I don't know anyone except Chuck Norris who is entitled to making such a claim. How long did you wait? 😉
rgaudin commentedon Apr 28, 2026
I think we should first try to rule-out a scraperlib issue since there is a mechanism in place around this
See https://github.com/openzim/python-scraperlib/blob/main/src/zimscraperlib/zim/creator.py#L405
The printed stack trace indicates the exception was raised while it shouldn't: it matches the expected pattern
veloman-yunkan commentedon Apr 28, 2026
I can't use the latest version of zimscraperlib since I am on python 3.10. Trying to reproduce it via the equivalentish libzim only script was not successful:
BTW, what is your version of
libzim? I tried 9.4.1, 9.5.1, 9.6.0 and current main.benoit74 commentedon Apr 28, 2026
9.5.1ATMbenoit74 commentedon Apr 28, 2026
Thank you. Transferring to scraperlib for the time being.
2 remaining items
benoit74 commentedon Oct 20, 2026
If someone want to reproduce in python-scraperlib and in pure libzim and confirm where the hangs occurs, you're welcomed
pawank925 commentedon Nov 4, 2026
@benoit74 Confirmed the hang in python-scraperlib. The duplicate exception is expected (
duplicate_okdefaults to False); the deadlock happens at interpreter exit: the main thread holds the GIL inzim::Creator's destructor while joining a worker thread that is insideCluster::close()calling back into Python throughPyGILState_Ensure. It needs pending cluster data, which matches the "fill at least one cluster" observation. The libzim-only script above does not hang becausewith creator:finishes the creation, and the binding releases the GIL duringfinishZimCreation(), so the workers are already drained.PR #378 makes an abandoned
Creatorfinish with the GIL released and then remove the resulting ZIM, with an atexit sweep for creators that are never finished, exactly like the reported program. It includes a subprocess regression test that times out without the fix.Fix hang when a ZIM creation is abandoned before finishing
benoit74 commentedon Nov 5, 2026
I'm not totally convinced about #378 approach ; not saying it doesn't fix the bug (it does) but wondering if the bug is worth to fix.
The problem is that my original script in this issue first comment did not correctly cleaned stuff.
Not sure we want python-libzim to auto-magically fix these mistakes with a somewhat questionable decision (delete in-progress ZIM is "risky", might bite us at some point).
For instance if you are creating the a ZIP file and suddenly exit, and you did not used a context manager, then the ZIP file is still on disk and corrupted.
The difference is obviously the fact that here we are dead-locking.
Maybe we should just avoid the dead-lock.
@rgaudin WDYT?
rgaudin commentedon Nov 5, 2026
I don't think that's the case because of atexit handler calling abandon() which will delete the file.
This is tied to
workaround_nocancelI believe so you can already choose if you want this or not, right?benoit74 commentedon Nov 5, 2026
Indeed
Fix hang when a ZIM creation is abandoned before finishing