Skip to content

Commit 8bd9ff0

Browse files
authored
Ensure we delete media if we reject due to spam check (#17246)
Fixes up #17239 We need to keep the spam check within the `try/except` block. Also makes it so that we don't enter the top span twice. Also also ensures that we get the right thumbnail length.
1 parent 466f344 commit 8bd9ff0

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

changelog.d/17246.misc

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

synapse/media/media_repository.py

+5
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,11 @@ async def _generate_thumbnails(
10491049
finally:
10501050
t_byte_source.close()
10511051

1052+
# We flush and close the file to ensure that the bytes have
1053+
# been written before getting the size.
1054+
f.flush()
1055+
f.close()
1056+
10521057
t_len = os.path.getsize(fname)
10531058

10541059
# Write to database

synapse/media/media_storage.py

+27-32
Original file line numberDiff line numberDiff line change
@@ -137,42 +137,37 @@ async def store_into_file(
137137
dirname = os.path.dirname(fname)
138138
os.makedirs(dirname, exist_ok=True)
139139

140-
main_media_repo_write_trace_scope = start_active_span(
141-
"writing to main media repo"
142-
)
143-
main_media_repo_write_trace_scope.__enter__()
144-
145-
with main_media_repo_write_trace_scope:
146-
try:
140+
try:
141+
with start_active_span("writing to main media repo"):
147142
with open(fname, "wb") as f:
148143
yield f, fname
149144

150-
except Exception as e:
151-
try:
152-
os.remove(fname)
153-
except Exception:
154-
pass
155-
156-
raise e from None
157-
158-
with start_active_span("writing to other storage providers"):
159-
spam_check = (
160-
await self._spam_checker_module_callbacks.check_media_file_for_spam(
161-
ReadableFileWrapper(self.clock, fname), file_info
145+
with start_active_span("writing to other storage providers"):
146+
spam_check = (
147+
await self._spam_checker_module_callbacks.check_media_file_for_spam(
148+
ReadableFileWrapper(self.clock, fname), file_info
149+
)
162150
)
163-
)
164-
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
165-
logger.info("Blocking media due to spam checker")
166-
# Note that we'll delete the stored media, due to the
167-
# try/except below. The media also won't be stored in
168-
# the DB.
169-
# We currently ignore any additional field returned by
170-
# the spam-check API.
171-
raise SpamMediaException(errcode=spam_check[0])
172-
173-
for provider in self.storage_providers:
174-
with start_active_span(str(provider)):
175-
await provider.store_file(path, file_info)
151+
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
152+
logger.info("Blocking media due to spam checker")
153+
# Note that we'll delete the stored media, due to the
154+
# try/except below. The media also won't be stored in
155+
# the DB.
156+
# We currently ignore any additional field returned by
157+
# the spam-check API.
158+
raise SpamMediaException(errcode=spam_check[0])
159+
160+
for provider in self.storage_providers:
161+
with start_active_span(str(provider)):
162+
await provider.store_file(path, file_info)
163+
164+
except Exception as e:
165+
try:
166+
os.remove(fname)
167+
except Exception:
168+
pass
169+
170+
raise e from None
176171

177172
async def fetch_media(self, file_info: FileInfo) -> Optional[Responder]:
178173
"""Attempts to fetch media described by file_info from the local cache

0 commit comments

Comments
 (0)