Compare commits

...

4 Commits

Author SHA1 Message Date
Erik Johnston
5e15d598ce Close the file before trying to read file size 2024-05-30 10:56:14 +01:00
Erik Johnston
5d71a6c9b9 Force flush + close of FD 2024-05-30 10:36:04 +01:00
Erik Johnston
152f998798 Newsfile 2024-05-30 10:30:57 +01:00
Erik Johnston
e0e0ed35ec Ensure we delete media if we reject due to spam check
Fixes up #17239
2024-05-30 10:14:18 +01:00
3 changed files with 34 additions and 31 deletions

1
changelog.d/17246.misc Normal file
View File

@@ -0,0 +1 @@
Fix errors in logs about closing incorrect logging contexts when media gets rejected by a module.

View File

@@ -1049,6 +1049,11 @@ class MediaRepository:
finally:
t_byte_source.close()
# We flush and close the file to ensure that the bytes have
# been written before getting the size.
f.flush()
f.close()
t_len = os.path.getsize(fname)
# Write to database

View File

@@ -137,42 +137,39 @@ class MediaStorage:
dirname = os.path.dirname(fname)
os.makedirs(dirname, exist_ok=True)
main_media_repo_write_trace_scope = start_active_span(
"writing to main media repo"
)
main_media_repo_write_trace_scope.__enter__()
with main_media_repo_write_trace_scope:
try:
try:
with start_active_span("writing to main media repo"):
with open(fname, "wb") as f:
yield f, fname
f.flush()
f.close()
except Exception as e:
try:
os.remove(fname)
except Exception:
pass
raise e from None
with start_active_span("writing to other storage providers"):
spam_check = (
await self._spam_checker_module_callbacks.check_media_file_for_spam(
ReadableFileWrapper(self.clock, fname), file_info
with start_active_span("writing to other storage providers"):
spam_check = (
await self._spam_checker_module_callbacks.check_media_file_for_spam(
ReadableFileWrapper(self.clock, fname), file_info
)
)
)
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
logger.info("Blocking media due to spam checker")
# Note that we'll delete the stored media, due to the
# try/except below. The media also won't be stored in
# the DB.
# We currently ignore any additional field returned by
# the spam-check API.
raise SpamMediaException(errcode=spam_check[0])
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
logger.info("Blocking media due to spam checker")
# Note that we'll delete the stored media, due to the
# try/except below. The media also won't be stored in
# the DB.
# We currently ignore any additional field returned by
# the spam-check API.
raise SpamMediaException(errcode=spam_check[0])
for provider in self.storage_providers:
with start_active_span(str(provider)):
await provider.store_file(path, file_info)
for provider in self.storage_providers:
with start_active_span(str(provider)):
await provider.store_file(path, file_info)
except Exception as e:
try:
os.remove(fname)
except Exception:
pass
raise e from None
async def fetch_media(self, file_info: FileInfo) -> Optional[Responder]:
"""Attempts to fetch media described by file_info from the local cache