question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Avoid filesystem writes when there are no bytes to write.

See original GitHub issue

Description

https://github.com/astropy/astropy/blob/main/astropy/io/fits/hdu/table.py#L939 which pads FITS binary tables that variable-length columns out to the necessary multiple of 2880, can be triggered even when data._gap == 0. That is, fileobj.write(b'') is not a no-op.

This padding may also be needed if a table or image is not itself an exact multiple of 2880 bytes, so it may be possible to trigger this even when there are no variable-length columns.

Expected behavior

Don’t trigger a filesystem write if there is nothing to write:

if data._gap > 0:
    fileobj.write((data._gap * '\0').encode('ascii'))

Actual behavior

The Lustre filesystem has a known issue that is triggered when zero-length writes are attempted. This bug report is an attempt to provide a workaround, and in general raise consciousness of corner cases that can happen in certain filesystems.

EDIT: Permalink to the code below

https://github.com/astropy/astropy/blob/3f8cf27b87f2e10f7389190971651cd33178bd7d/astropy/io/fits/hdu/table.py#L939

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
weaverba137commented, Jul 17, 2021

In principle the fix is petty simple:

if data._gap > 0:
    fileobj.write((data._gap * '\0').encode('ascii'))

The trick will be to write a unit test that exercises this. That’s been holding me up.

1reaction
dhomeiercommented, Jul 17, 2021

The Python docs say the standard open is in fact an alias to io.open; anyway fits.open also works with filehandles like

import io
fh = io.open('file.fits', mode='r+b')
hdulist = fits.open(fh, mode='u')

so if you replace fh with a handle created by such a custom opener, you should be done – ideally 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does writing to a file with no seeks flush bytes sequentially on ...
I'm writing JSON files. This means that for each '{' character, there is a corresponding '}' and the last byte in the file...
Read more >
How can I prevent high-volume filesystem writes ... - Server Fault
In several contexts, I've seen a behavior on Linux systems in which large volumes of filesystem write operations (e.g., many gigabytes of  ......
Read more >
Is there a way to force the write command to block until all ...
You have two options: Use open with the O_SYNC flag. From the Manpage: O_SYNC Write operations on the file will complete according to...
Read more >
Why buffered writes are sometimes stalled
Answer: 1. write() does disk read when needed. To avoid this issue you need to append a file, not overwrite. Or use OS...
Read more >
Write to Descriptor - IBM
Reading and writing to files with the Network File System relies on byte-range locking to guarantee data integrity. To prevent data inconsistency, use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found