Skip to content

Commit fdbe66a

Browse files
committed
gradle: Fix c-ares initialisation error
With a sufficiently large number of URLs to process, the script would fail with: pycares.AresError: Failed to initialize c-ares channel which usually means that process resources are exhausted. Use a single HTTP session for aiohttp, as recommended in the aiohttp docs: > Don’t create a session per request. [...] > [M]aking a session for every request is a very bad idea. > A session contains a connection pool inside. See https://docs.aiohttp.org/en/stable/client_quickstart.html
1 parent 7ac335e commit fdbe66a

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

gradle/flatpak-gradle-generator.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@
1717
'linux-aarch_32': 'arm'
1818
}
1919

20-
async def get_remote_sha256(url):
20+
async def get_remote_sha256(http_session, url):
2121
logging.info(f"started sha256({url})")
2222
sha256 = hashlib.sha256()
23-
async with aiohttp.ClientSession(raise_for_status=True) as http_session:
24-
async with http_session.get(url) as response:
25-
while True:
26-
data = await response.content.read(4096)
27-
if not data:
28-
break
29-
sha256.update(data)
23+
async with http_session.get(url) as response:
24+
while True:
25+
data = await response.content.read(4096)
26+
if not data:
27+
break
28+
sha256.update(data)
3029
logging.info(f"done sha256({url})")
3130
return sha256.hexdigest()
3231

33-
async def parse_url(url, destdir, arch=None):
32+
async def parse_url(http_session, url, destdir, arch=None):
3433
ret = [{ 'type': 'file',
3534
'url': url,
36-
'sha256': await get_remote_sha256(url),
35+
'sha256': await get_remote_sha256(http_session, url),
3736
'dest': destdir, }]
3837
if arch:
3938
ret[0]['only-arches'] = [arch]
@@ -50,10 +49,12 @@ def arch_for_url(url, urls_arch):
5049
async def parse_urls(urls, urls_arch, destdir):
5150
sources = []
5251
sha_coros = []
52+
http_session = aiohttp.ClientSession()
5353
for url in urls:
5454
arch = arch_for_url(url, urls_arch)
55-
sha_coros.append(parse_url(str(url), destdir, arch))
55+
sha_coros.append(parse_url(http_session, str(url), destdir, arch))
5656
sources.extend(sum(await asyncio.gather(*sha_coros), []))
57+
await http_session.close()
5758
return sources
5859

5960
def gradle_arch_to_flatpak_arch(arch):

0 commit comments

Comments
 (0)