Fix async/await calls for broken media providers. (#8027)

This commit is contained in:
Patrick Cloke 2020-08-04 09:44:25 -04:00 committed by GitHub
parent 88a3ff12f0
commit 8ff2deda72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 22 deletions

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

@ -0,0 +1 @@
Convert various parts of the codebase to async/await.

View File

@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import contextlib import contextlib
import inspect
import logging import logging
import os import os
import shutil import shutil
@ -30,7 +29,7 @@ from .filepath import MediaFilePaths
if TYPE_CHECKING: if TYPE_CHECKING:
from synapse.server import HomeServer from synapse.server import HomeServer
from .storage_provider import StorageProvider from .storage_provider import StorageProviderWrapper
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -50,7 +49,7 @@ class MediaStorage(object):
hs: "HomeServer", hs: "HomeServer",
local_media_directory: str, local_media_directory: str,
filepaths: MediaFilePaths, filepaths: MediaFilePaths,
storage_providers: Sequence["StorageProvider"], storage_providers: Sequence["StorageProviderWrapper"],
): ):
self.hs = hs self.hs = hs
self.local_media_directory = local_media_directory self.local_media_directory = local_media_directory
@ -115,11 +114,7 @@ class MediaStorage(object):
async def finish(): async def finish():
for provider in self.storage_providers: for provider in self.storage_providers:
# store_file is supposed to return an Awaitable, but guard await provider.store_file(path, file_info)
# against improper implementations.
result = provider.store_file(path, file_info)
if inspect.isawaitable(result):
await result
finished_called[0] = True finished_called[0] = True
@ -153,11 +148,7 @@ class MediaStorage(object):
return FileResponder(open(local_path, "rb")) return FileResponder(open(local_path, "rb"))
for provider in self.storage_providers: for provider in self.storage_providers:
res = provider.fetch(path, file_info) # type: Any res = await provider.fetch(path, file_info) # type: Any
# Fetch is supposed to return an Awaitable[Responder], but guard
# against improper implementations.
if inspect.isawaitable(res):
res = await res
if res: if res:
logger.debug("Streaming %s from %s", path, provider) logger.debug("Streaming %s from %s", path, provider)
return res return res
@ -184,11 +175,7 @@ class MediaStorage(object):
os.makedirs(dirname) os.makedirs(dirname)
for provider in self.storage_providers: for provider in self.storage_providers:
res = provider.fetch(path, file_info) # type: Any res = await provider.fetch(path, file_info) # type: Any
# Fetch is supposed to return an Awaitable[Responder], but guard
# against improper implementations.
if inspect.isawaitable(res):
res = await res
if res: if res:
with res: with res:
consumer = BackgroundFileConsumer( consumer = BackgroundFileConsumer(

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import inspect
import logging import logging
import os import os
import shutil import shutil
@ -88,12 +89,18 @@ class StorageProviderWrapper(StorageProvider):
return None return None
if self.store_synchronous: if self.store_synchronous:
return await self.backend.store_file(path, file_info) # store_file is supposed to return an Awaitable, but guard
# against improper implementations.
result = self.backend.store_file(path, file_info)
if inspect.isawaitable(result):
return await result
else: else:
# TODO: Handle errors. # TODO: Handle errors.
def store(): async def store():
try: try:
return self.backend.store_file(path, file_info) result = self.backend.store_file(path, file_info)
if inspect.isawaitable(result):
return await result
except Exception: except Exception:
logger.exception("Error storing file") logger.exception("Error storing file")
@ -101,7 +108,11 @@ class StorageProviderWrapper(StorageProvider):
return None return None
async def fetch(self, path, file_info): async def fetch(self, path, file_info):
return await self.backend.fetch(path, file_info) # store_file is supposed to return an Awaitable, but guard
# against improper implementations.
result = self.backend.fetch(path, file_info)
if inspect.isawaitable(result):
return await result
class FileStorageProviderBackend(StorageProvider): class FileStorageProviderBackend(StorageProvider):