Media upload fails with `ValueError: seek of closed file`

See original GitHub issue

Description

Media uploads are failing.

Steps to reproduce

Uploading any file in Riot (any platform) results in the upload failing.

image
2019-10-10 14:57:47,521 - synapse.http.server - 109 - ERROR - POST-329537- Failed handle request via 'UploadResource': <XForwardedForRequest at 0x7f1fb281c240 method='POST' uri='/_matrix/media/v1/upload?filename=ima_424b122.png' clientproto='HTTP/1.0' site=8008>
Traceback (most recent call last):
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/synapse/http/server.py", line 77, in wrapped_request_handler
    await h(self, request)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/synapse/rest/media/v1/upload_resource.py", line 82, in _async_render_POST
    media_type, upload_name, request.content, content_length, requester.user
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/twisted/python/failure.py", line 512, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/synapse/rest/media/v1/media_repository.py", line 160, in create_content
    fname = yield self.media_storage.store_file(content, file_info)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/twisted/python/failure.py", line 512, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/synapse/rest/media/v1/media_storage.py", line 68, in store_file
    self.hs.get_reactor(), _write_file_synchronously, source, f
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/twisted/python/threadpool.py", line 250, in inContext
    result = inContext.theWork()
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/twisted/python/threadpool.py", line 266, in <lambda>
    inContext.theWork = lambda: context.call(ctx, func, *args, **kw)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/twisted/python/context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/twisted/python/context.py", line 85, in callWithContext
    return func(*args,**kw)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/synapse/logging/context.py", line 716, in g
    return f(*args, **kwargs)
  File "/opt/venvs/matrix-synapse/lib/python3.6/site-packages/synapse/rest/media/v1/media_storage.py", line 244, in _write_file_synchronously
    source.seek(0)  # Ensure we read from the start of the file
ValueError: seek of closed file
2019-10-10 14:57:47,578 - synapse.http.server - 458 - WARNING - POST-329537- Not sending response to request <XForwardedForRequest at 0x7f1fb281c240 method='POST' uri='/_matrix/media/v1/upload?filename=ima_424b122.png' clientproto='HTTP/1.0' site=8008>, already disconnected.

I also see similar errors ending in ValueError: I/O operation on closed file. on some uploads. I can upload the homeserver.log if needed.

Version information

  • Homeserver: chat.privacytools.io

If not matrix.org:

  • Version: 1.4.0

  • Install method: apt

  • Platform: Ubuntu 18.04.3 LTS, running in an LXC container. The Matrix HS is behind an nginx reverse proxy on another container/

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:28 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
squahtxcommented, Nov 17, 2021

I’m wholly unable to reproduce the issue. The error in the OP means that the client, reverse proxy, or anything in between aborted the request before Synapse received all the data and saved it. If you’re having issues with a self-hosted instance of Synapse, it’d be extremely helpful to provide:

  • The size of the file being uploaded
  • Whether uploading the same file always fails or sometimes succeeds
  • The client version you’re using
  • The speed of the internet connection being used to upload the file
  • Whether you’re using mobile data or Wi-Fi
  • Whether there’s a reverse proxy in front of Synapse
    • The values of proxy_request_buffering and client_max_body_size in the config if you’re using nginx
  • The Synapse version you’re using
0reactions
Hierscommented, Jun 26, 2022

I was having this problem too. No file, no matter how big or small, could be uploaded. I eventually found the problem was in the homeserver.yaml “media_store_path” config. It was set to /var/lib/synapse/media_store, and I was running synapse as another user (not synapse) who didn’t have write access to that directory. Either changing the directory to somewhere you have write permission or having synapse be run as the synapse user will fix it if this is your case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

valueerror: seek of closed file - You.com | The Search Engine You ...
Media uploads are failing. Steps to reproduce. Uploading any file in Riot (any platform) results in the upload failing. image.
Read more >
seek of closed file Working on PyPDF2 and getting this error ...
PdfFileReader expects a seekable, open, steam. It does not load the entire file into memory, so you have to keep it open to...
Read more >
Python ValueError: I/O operation on closed file Solution
The “ValueError : I/O operation on closed file” error is raised when you try to read from or write to a file that...
Read more >
Managing files - Django documentation
This document describes Django's file access APIs for files such as those ... image = Image.open(car.photo) # Raises ValueError: seek of closed file....
Read more >
[Solved] ValueError: I/O operation on closed file. - Python Pool
A quick fix is to downgrade to a version openpyxl==2.5.11 where this issue isn't present. ValueError: I/O operation on closed file. Python, ...
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