[GH-ISSUE #310] Zip file not properly closed after file write error #250

Closed
opened 2026-05-05 12:02:12 -06:00 by gitea-mirror · 9 comments
Owner

Originally created by @matthiasstraka on GitHub (Sep 24, 2020).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/310

Originally assigned to: @jmcnamara on GitHub.

I am having an issue with writing an xlsx file to a disk that is already full. A write error is reported (in _add_file_to_zip) but the corresponding zip file remains open when reporting the error to the caller of workbook_close. This prevents me from erasing a failed xlsx file.

How to reproduce:

  • Create small RAM disk (or choose disk with low remaining storage space)
  • Write large XLSX file to it
Originally created by @matthiasstraka on GitHub (Sep 24, 2020). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/310 Originally assigned to: @jmcnamara on GitHub. I am having an issue with writing an xlsx file to a disk that is already full. A write error is reported (in _add_file_to_zip) but the corresponding zip file remains open when reporting the error to the caller of workbook_close. This prevents me from erasing a failed xlsx file. How to reproduce: - Create small RAM disk (or choose disk with low remaining storage space) - Write large XLSX file to it
gitea-mirror 2026-05-05 12:02:12 -06:00
Author
Owner

@jmcnamara commented on GitHub (Sep 24, 2020):

Thanks for the report. I'll look into it.

<!-- gh-comment-id:698296719 --> @jmcnamara commented on GitHub (Sep 24, 2020): Thanks for the report. I'll look into it.
Author
Owner

@jmcnamara commented on GitHub (Sep 24, 2020):

Can you try the fix on the gh310 branch to see if it resolves your issue.

<!-- gh-comment-id:698349095 --> @jmcnamara commented on GitHub (Sep 24, 2020): Can you try the fix on the gh310 branch to see if it resolves your issue.
Author
Owner

@matthiasstraka commented on GitHub (Sep 25, 2020):

Thanks, this looks better. Sorry for not fixing it myself as I do not have the time to check for side effects at the moment.
I would also suggest swapping fclose(worksheet->file); and RETURN_ON_ERROR(err); in _write_worksheet_files and maybe call zipCloseFileInZip(self->zipfile); in the error paths of _add_file_to_zip or clean up zipfile in an approriate way.

<!-- gh-comment-id:698758960 --> @matthiasstraka commented on GitHub (Sep 25, 2020): Thanks, this looks better. Sorry for not fixing it myself as I do not have the time to check for side effects at the moment. I would also suggest swapping `fclose(worksheet->file);` and `RETURN_ON_ERROR(err);` in __write_worksheet_files_ and maybe call `zipCloseFileInZip(self->zipfile);` in the error paths of __add_file_to_zip_ or clean up zipfile in an approriate way.
Author
Owner

@jmcnamara commented on GitHub (Sep 25, 2020):

I would also suggest swapping fclose(worksheet->file); and RETURN_ON_ERROR(err); in _write_worksheet_files and maybe call zipCloseFileInZip(self->zipfile); in the error paths of _add_file_to_zip or clean up zipfile in an approriate way.

Good suggestions. I'll look into that.

<!-- gh-comment-id:698761272 --> @jmcnamara commented on GitHub (Sep 25, 2020): > I would also suggest swapping `fclose(worksheet->file);` and `RETURN_ON_ERROR(err);` in __write_worksheet_files_ and maybe call `zipCloseFileInZip(self->zipfile);` in the error paths of __add_file_to_zip_ or clean up zipfile in an approriate way. Good suggestions. I'll look into that.
Author
Owner

@jmcnamara commented on GitHub (Sep 25, 2020):

I've force pushed an updated version onto the same branch: 2c8cf48

Try it out for your failing test case if you get a chance.

<!-- gh-comment-id:698776793 --> @jmcnamara commented on GitHub (Sep 25, 2020): I've force pushed an updated version onto the same branch: 2c8cf48 Try it out for your failing test case if you get a chance.
Author
Owner

@matthiasstraka commented on GitHub (Sep 25, 2020):

I tested you commits and can happily report that both the current temporary file and the xlsx-zip file are closed when my disk runs full. Thus, I can delete the file after writing failed and no temporary files stay around. This issue is fixed for me.

<!-- gh-comment-id:698836228 --> @matthiasstraka commented on GitHub (Sep 25, 2020): I tested you commits and can happily report that both the current temporary file and the xlsx-zip file are closed when my disk runs full. Thus, I can delete the file after writing failed and no temporary files stay around. This issue is fixed for me.
Author
Owner

@jmcnamara commented on GitHub (Sep 25, 2020):

Cool thanks for the feedback @matthiasstraka

I'll do a bit more testing and merge it up to main.

<!-- gh-comment-id:698840698 --> @jmcnamara commented on GitHub (Sep 25, 2020): Cool thanks for the feedback @matthiasstraka I'll do a bit more testing and merge it up to main.
Author
Owner

@jmcnamara commented on GitHub (Sep 25, 2020):

@matthiasstraka Thanks for testing.

There were still some possible, but unlikely, paths that could return an error without a cleanup. I think all of them are covered now.

When you get a chance can you test once more with the code on the same branch: dfbc07c

No rush.

<!-- gh-comment-id:699083621 --> @jmcnamara commented on GitHub (Sep 25, 2020): @matthiasstraka Thanks for testing. There were still some possible, but unlikely, paths that could return an error without a cleanup. I think all of them are covered now. When you get a chance can you test once more with the code on the same branch: dfbc07c No rush.
Author
Owner

@jmcnamara commented on GitHub (Oct 2, 2020):

Fixed and merged to main. Thanks for the report.

<!-- gh-comment-id:702925160 --> @jmcnamara commented on GitHub (Oct 2, 2020): Fixed and merged to main. Thanks for the report.
Sign in to join this conversation.
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: github-starred/libxlsxwriter#250
No description provided.