Skip to content

Program hangs forever when adding two items at same path #300

Closed

Description

@benoit74

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

  1. added
    bug
    question
    on Apr 28, 2026
  2. changed the title [-]Program hangs forever when add two items at same path[/-] [+]Program hangs forever when adding two items at same path[/+] on Apr 28, 2026
  3. benoit74 commented on Apr 28, 2026

    @benoit74
    CollaboratorAuthor

    Edit: fixed wrong word in last paragraph

  4. veloman-yunkan commented on Apr 28, 2026

    @veloman-yunkan

    What is abnormal is that the program hangs forever,

    I don't know anyone except Chuck Norris who is entitled to making such a claim. How long did you wait? 😉

  5. rgaudin commented on Apr 28, 2026

    @rgaudin
    Member

    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

  6. veloman-yunkan commented on Apr 28, 2026

    @veloman-yunkan

    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:

    import libzim
    from libzim.writer import Creator, StringProvider, Hint
    
    DEFAULT_DEV_ZIM_METADATA = {
        "Name": "Test Name",
        "Title": "Test Title",
        "Creator": "Test Creator",
        "Publisher": "Test Publisher",
        "Date": "2023-01-01",
        "Description": "Test Description",
        "Language": "fra",
    }
    
    FRONT_ARTICLE_HINTS = {Hint.FRONT_ARTICLE : True}
    
    class StaticItem(libzim.writer.Item):
        def __init__(self, **kwargs):
            super().__init__()
            for k, v in kwargs.items():
                setattr(self, k, v)
    
        def get_path(self) -> str:
            return getattr(self, "path", "")
    
        def get_title(self) -> str:
            return getattr(self, "title", "")
    
        def get_mimetype(self) -> str:
            return getattr(self, "mimetype", "text/html")
    
        def get_contentprovider(self) -> libzim.writer.ContentProvider:
            return StringProvider(content=getattr(self, "content", ""))
    
        def get_hints(self) -> dict[Hint, int]:
            return getattr(self, "hints", {})
    
    creator = Creator("tests.zim")
    creator.set_mainpath("index.html")
    #creator.config_indexing(False, "eng")
    with creator:
        for name, value in DEFAULT_DEV_ZIM_METADATA.items():
            creator.add_metadata(name, value)
    
        creator.add_item(StaticItem(path="index.html",
                                    title="Main Page",
                                    content="Main",
                                    hints=FRONT_ARTICLE_HINTS))
    
        for i in range(10):
            creator.add_item(StaticItem(path=f"content{i}", content="P" * 1000000))
    
        creator.add_item(StaticItem(path="conflict", content="Foo"))
        creator.add_item(StaticItem(path="conflict", content="Bar"))

    BTW, what is your version of libzim? I tried 9.4.1, 9.5.1, 9.6.0 and current main.

  7. benoit74 commented on Apr 28, 2026

    @benoit74
    CollaboratorAuthor

    9.5.1 ATM

  8. transferred this issue fromopenzim/libzimon Apr 28, 2026
  9. benoit74 commented on Apr 28, 2026

    @benoit74
    CollaboratorAuthor

    Thank you. Transferring to scraperlib for the time being.

  10. added this to the 5.5.0 milestone on May 28, 2026
  11. 2 remaining items

  12. added theissue type on Oct 1, 2026
  13. benoit74 commented on Oct 20, 2026

    @benoit74
    CollaboratorAuthor

    If someone want to reproduce in python-scraperlib and in pure libzim and confirm where the hangs occurs, you're welcomed

  14. modified the milestones: 5.5.0, 5.6.0 on Oct 20, 2026
  15. pawank925 commented on Nov 4, 2026

    @pawank925
    Contributor

    @benoit74 Confirmed the hang in python-scraperlib. The duplicate exception is expected (duplicate_ok defaults to False); the deadlock happens at interpreter exit: the main thread holds the GIL in zim::Creator's destructor while joining a worker thread that is inside Cluster::close() calling back into Python through PyGILState_Ensure. It needs pending cluster data, which matches the "fill at least one cluster" observation. The libzim-only script above does not hang because with creator: finishes the creation, and the binding releases the GIL during finishZimCreation(), so the workers are already drained.

    PR #378 makes an abandoned Creator finish 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.

  16. added a commit that references this issue on Nov 4, 2026
    9c6cf43
  17. benoit74 commented on Nov 5, 2026

    @benoit74
    CollaboratorAuthor

    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?

  18. rgaudin commented on Nov 5, 2026

    @rgaudin
    Member

    20. 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.

    I don't think that's the case because of atexit handler calling abandon() which will delete the file.

    delete in-progress ZIM is "risky", might bite us at some point

    This is tied to workaround_nocancel I believe so you can already choose if you want this or not, right?

  19. benoit74 commented on Nov 5, 2026

    @benoit74
    CollaboratorAuthor

    This is tied to workaround_nocancel I believe so you can already choose if you want this or not, right?

    Indeed

  20. added a commit that references this issue on Nov 5, 2026
    fcc6750
  21. modified the milestones: 5.6.0, 5.5.2 on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions