# Bug Report: Cannot close object, library is destroyed. This may cause a memory leak!

**URL:** <https://support.prodi.gy/t/bug-report-cannot-close-object-library-is-destroyed-this-may-cause-a-memory-leak/7470>\
**Category:** Uncategorized\
**Tags:** bug, solved\
**Created:** [December 23, 2024, 12:39am UTC](https://support.prodi.gy/t/bug-report-cannot-close-object-library-is-destroyed-this-may-cause-a-memory-leak/7470 "2024-12-23T00:39:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmnxprss](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/dmnxprss/32/4541_2.png) [@dmnxprss](https://support.prodi.gy/u/dmnxprss)\
**Post date:** [December 23, 2024, 12:39am UTC](https://support.prodi.gy/t/bug-report-cannot-close-object-library-is-destroyed-this-may-cause-a-memory-leak/7470/1 "2024-12-23T00:39:50Z")

</div>

**🐛 Bug description:**  
When trying to run a pdf through a Prodigy PDF module (eg. pdf.layout.fetch) the system will start processing the pdf, but ultimately error out with the below.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/prodigy/original/2X/0/0a6565184f02cd770f301f629b75ccc5e4e150c9.png)

Through online research, several users have received similar errors (outside of Prodigy use) with a common solution being to downgrade transformers==4.38.2

However, doing this leads to a dependency conflict as docling-ibm-models 3.1.0 requires transformers\<5.0.0,\>=4.42.0.

**🚶🏽‍♂️Reproduction steps:**  
Clean install of prodigy 1.17.3 and prodigy pdf and running a pdf through it.

**🖥 Environment variables:**

```python
Version 1.17.3
License Type Prodigy Personal
Location D:\Programs\anaconda3\envs\env311_nlp\Lib\site-packages\prodigy
Prodigy Home C:\Users\sinad\.prodigy       
Platform Windows-10-10.0.19045-SP0     
Python Version 3.11.11
spaCy Version 3.7.5
Database Name SQLite
Database Id sqlite
Total Datasets 3
Total Sessions 25

```

---

<div class="post-metadata">

**Author:** ![magdaaniol](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/magdaaniol/32/2787_2.png) [@magdaaniol](https://support.prodi.gy/u/magdaaniol)\
**Post date:** [December 23, 2024, 3:15pm UTC](https://support.prodi.gy/t/bug-report-cannot-close-object-library-is-destroyed-this-may-cause-a-memory-leak/7470/2 "2024-12-23T15:15:18Z")

</div>

Hi @dmnxprss,

The issue actually comes from a potential resource conflict between `pypdfium2` and `docling` when they try to access the same PDF file  
It can be fixed by explicitly closing the PDF file once it's not used anymore by `pypdfium2`.  
Could you check if version [0.4.1](https://github.com/explosion/prodigy-pdf/releases/tag/v0.4.1) of the plugin solves this issue?

Also a heads-up that while working on this issue I found a small bug in `spacy-layout` where the document layout pagination and span layout pagination might be mismatched. You also might watch for an update from `spacy-layout`( [PR](https://github.com/explosion/spacy-layout/pull/22))

---

<div class="post-metadata">

**Author:** ![dmnxprss](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/dmnxprss/32/4541_2.png) [@dmnxprss](https://support.prodi.gy/u/dmnxprss)\
**Post date:** [December 23, 2024, 3:31pm UTC](https://support.prodi.gy/t/bug-report-cannot-close-object-library-is-destroyed-this-may-cause-a-memory-leak/7470/3 "2024-12-23T15:31:40Z")

</div>

Hi @magdaaniol ,

Thanks for the quick reply.

Unfortunately, after updating to version 0.4.1 the issue (KeyError: 1) still remains. Additionally, I am testing on documents greater than 1 page to account for the separate potential spacy-layout issue.

---

<div class="post-metadata">

**Author:** ![magdaaniol](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/magdaaniol/32/2787_2.png) [@magdaaniol](https://support.prodi.gy/u/magdaaniol)\
**Post date:** [December 23, 2024, 3:45pm UTC](https://support.prodi.gy/t/bug-report-cannot-close-object-library-is-destroyed-this-may-cause-a-memory-leak/7470/4 "2024-12-23T15:45:39Z")

</div>

Thanks for the quick feedback!  
The `Cannot close object, library is destroyed` message is not showing up anymore, right?  
The Key Error is related to the `spacy-layout` issue. If there are layout spans on pg 1 of the PDF, they will cause the KeyError as pg 1 won't exist on the document layout.  
I'll let you know as soon as the patch to `spacy-layout` is released. Sorry about that!

---

<div class="post-metadata">

**Author:** ![dmnxprss](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/dmnxprss/32/4541_2.png) [@dmnxprss](https://support.prodi.gy/u/dmnxprss)\
**Post date:** [December 23, 2024, 3:53pm UTC](https://support.prodi.gy/t/bug-report-cannot-close-object-library-is-destroyed-this-may-cause-a-memory-leak/7470/5 "2024-12-23T15:53:22Z")

</div>

Apologies, you are correct.  
The the `Cannot close object, library is destroyed` message is no longer appearing. Thank you

For the moment I have resolved this locally utilizing the fix you suggested within spacy\_layout.

Thanks again

---

<div class="post-metadata">

**Author:** ![magdaaniol](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/magdaaniol/32/2787_2.png) [@magdaaniol](https://support.prodi.gy/u/magdaaniol)\
**Post date:** [December 24, 2024, 9:55am UTC](https://support.prodi.gy/t/bug-report-cannot-close-object-library-is-destroyed-this-may-cause-a-memory-leak/7470/6 "2024-12-24T09:55:43Z")

</div>

Hi @dmnxprss ,

Thanks for the update! Happy to report that we have already patched `spacy-layout`.  
You should be able upgrade to v0.0.11 in place to get the fix.  
In the meantime, we have also fixed one more outstanding issue in `prodigy-pdf` plugin, so it's recommended to upgrade this as well to v0.4.2. Thanks again for the report!
