Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 7a0323f

Browse files
committed
Fix async/await calls for broken media providers.
1 parent 6812509 commit 7a0323f

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

changelog.d/8027.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert various parts of the codebase to async/await.

synapse/rest/media/v1/media_storage.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import contextlib
16-
import inspect
1716
import logging
1817
import os
1918
import shutil
@@ -30,7 +29,7 @@
3029
if TYPE_CHECKING:
3130
from synapse.server import HomeServer
3231

33-
from .storage_provider import StorageProvider
32+
from .storage_provider import StorageProviderWrapper
3433

3534
logger = logging.getLogger(__name__)
3635

@@ -50,7 +49,7 @@ def __init__(
5049
hs: "HomeServer",
5150
local_media_directory: str,
5251
filepaths: MediaFilePaths,
53-
storage_providers: Sequence["StorageProvider"],
52+
storage_providers: Sequence["StorageProviderWrapper"],
5453
):
5554
self.hs = hs
5655
self.local_media_directory = local_media_directory
@@ -115,11 +114,7 @@ def store_into_file(self, file_info: FileInfo):
115114

116115
async def finish():
117116
for provider in self.storage_providers:
118-
# store_file is supposed to return an Awaitable, but guard
119-
# against improper implementations.
120-
result = provider.store_file(path, file_info)
121-
if inspect.isawaitable(result):
122-
await result
117+
await provider.store_file(path, file_info)
123118

124119
finished_called[0] = True
125120

@@ -153,11 +148,7 @@ async def fetch_media(self, file_info: FileInfo) -> Optional[Responder]:
153148
return FileResponder(open(local_path, "rb"))
154149

155150
for provider in self.storage_providers:
156-
res = provider.fetch(path, file_info) # type: Any
157-
# Fetch is supposed to return an Awaitable[Responder], but guard
158-
# against improper implementations.
159-
if inspect.isawaitable(res):
160-
res = await res
151+
res = await provider.fetch(path, file_info) # type: Any
161152
if res:
162153
logger.debug("Streaming %s from %s", path, provider)
163154
return res
@@ -184,11 +175,7 @@ async def ensure_media_is_in_local_cache(self, file_info: FileInfo) -> str:
184175
os.makedirs(dirname)
185176

186177
for provider in self.storage_providers:
187-
res = provider.fetch(path, file_info) # type: Any
188-
# Fetch is supposed to return an Awaitable[Responder], but guard
189-
# against improper implementations.
190-
if inspect.isawaitable(res):
191-
res = await res
178+
res = await provider.fetch(path, file_info) # type: Any
192179
if res:
193180
with res:
194181
consumer = BackgroundFileConsumer(

synapse/rest/media/v1/storage_provider.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import inspect
1617
import logging
1718
import os
1819
import shutil
@@ -88,20 +89,30 @@ async def store_file(self, path, file_info):
8889
return None
8990

9091
if self.store_synchronous:
91-
return await self.backend.store_file(path, file_info)
92+
# store_file is supposed to return an Awaitable, but guard
93+
# against improper implementations.
94+
result = self.backend.store_file(path, file_info)
95+
if inspect.isawaitable(result):
96+
return await result
9297
else:
9398
# TODO: Handle errors.
94-
def store():
99+
async def store():
95100
try:
96-
return self.backend.store_file(path, file_info)
101+
result = self.backend.store_file(path, file_info)
102+
if inspect.isawaitable(result):
103+
return await result
97104
except Exception:
98105
logger.exception("Error storing file")
99106

100107
run_in_background(store)
101108
return None
102109

103110
async def fetch(self, path, file_info):
104-
return await self.backend.fetch(path, file_info)
111+
# store_file is supposed to return an Awaitable, but guard
112+
# against improper implementations.
113+
result = self.backend.fetch(path, file_info)
114+
if inspect.isawaitable(result):
115+
return await result
105116

106117

107118
class FileStorageProviderBackend(StorageProvider):

0 commit comments

Comments
 (0)