about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <cnx@loang.net>2024-01-10 04:00:44 +0900
committerNguyễn Gia Phong <cnx@loang.net>2024-01-10 07:35:51 +0900
commite440e799c139b967a88cacc298a8d0e735dcaff6 (patch)
tree85ac4ce0e8d4d9166b68fd142a9f69ad52a796f4
parent4aba8c380b57ee05415dc0b275b8ce9dd8b7dafb (diff)
downloadfead-e440e799c139b967a88cacc298a8d0e735dcaff6.tar.gz
Silence errors due to task cancelation
-rwxr-xr-xsrc/fead.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/fead.py b/src/fead.py
index 370b623..5987020 100755
--- a/src/fead.py
+++ b/src/fead.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 # Advert generator from web feeds
-# Copyright (C) 2022  Nguyễn Gia Phong
+# Copyright (C) 2022, 2024  Nguyễn Gia Phong
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published
@@ -18,7 +18,7 @@
 __version__ = '0.1.3'
 
 from argparse import ArgumentParser, FileType, HelpFormatter
-from asyncio import gather, open_connection, run
+from asyncio import CancelledError, gather, open_connection, run
 from collections import namedtuple
 from datetime import datetime
 from email.utils import parsedate_to_datetime
@@ -150,10 +150,14 @@ async def fetch(raw_url):
         reader, writer = await open_connection(url.hostname, 80)
     else:
         raise ValueError(f'unsupported URL scheme: {url.scheme}')
-    writer.write(REQUEST.format(f"{url.path or '/'}?{url.query}",
-                                url.hostname).encode())
-    response = HTTPResponse(BytesSocket(await reader.read()))
-    writer.close()
+    try:
+        writer.write(REQUEST.format(f"{url.path or '/'}?{url.query}",
+                                    url.hostname).encode())
+        response = HTTPResponse(BytesSocket(await reader.read()))
+    except CancelledError:
+        return None  # silence propagation
+    finally:
+        writer.close()
 
     response.begin()
     with response: