Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read_data(_, file) closes file prematurely #164

Open
jackriccomini opened this issue Jan 17, 2025 · 0 comments · May be fixed by #165
Open

read_data(_, file) closes file prematurely #164

jackriccomini opened this issue Jan 17, 2025 · 0 comments · May be fixed by #165

Comments

@jackriccomini
Copy link

jackriccomini commented Jan 17, 2025

I'm receiving NRRD files over a network. I want to use pynrrd to parse and validate those files while they're in memory before they've been saved to disk.

I am passing in a file object to nrrd.read_data(). I want to keep using that file object afterwards but nrrd.read_data(_, file) closes the file. I can't reopen the file after passing it to nrrd.read_data() because I haven't yet persisted it to disk. Once the file has been closed, it's gone.

Minimal reproduction using Flask:

from flask import Flask, request
import nrrd

app = Flask(__name__)

@app.route("/process", methods=["POST"])
def process_file():
    file = request.files["file"].stream

    is_file_ok = check_nrrd(file)

    if not is_file_ok:
        return "some error", 400

    file.seek(0)
    do_something_with_file(file)

    return "uploaded"

def check_nrrd(file):
    """Check that the NRRD conforms to our expectations"""
    header = nrrd.read_header(file)
    data = nrrd.read_data(header, file)

    # perform some checks
    file_is_good = ...
    
    return file_is_good

def do_something_with_file(file):
    print(file.read())

Tested with:

$ curl -F 'file=@test.nrrd' http://localhost:5000/process

Log:

127.0.0.1 - - [17/Jan/2025 17:23:11] "POST /process HTTP/1.1" 500 -
[2025-01-17 17:28:39,601] ERROR in app: Exception on /process [POST]
Traceback (most recent call last):
  File "/Users/JackRiccomini/.pyenv/versions/3.12.7/lib/python3.12/site-packages/flask/app.py", line 1511, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/JackRiccomini/.pyenv/versions/3.12.7/lib/python3.12/site-packages/flask/app.py", line 919, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/JackRiccomini/.pyenv/versions/3.12.7/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/JackRiccomini/.pyenv/versions/3.12.7/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/min.py", line 15, in process_file
    file.seek(0)
  File "/Users/JackRiccomini/.pyenv/versions/3.12.7/lib/python3.12/tempfile.py", line 828, in seek
    return self._file.seek(*args)
           ^^^^^^^^^^^^^^^^^^^^^^
ValueError: I/O operation on closed file.
jackriccomini pushed a commit to jackriccomini/pynrrd that referenced this issue Jan 17, 2025
@jackriccomini jackriccomini linked a pull request Jan 17, 2025 that will close this issue
@jackriccomini jackriccomini changed the title read_data(file) closes file prematurely read_data(_, file) closes file prematurely Jan 17, 2025
jackriccomini pushed a commit to jackriccomini/pynrrd that referenced this issue Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant