Skip to content

Commit f64dc9c

Browse files
odesenfansnesitor
authored andcommitted
fix: timeout error caused by long IPFS file stat operation
The `ipfs/add_file` endpoint fails randomly because of a 5 second timeout when stat-ing the file to retrieve its size. Increased the timeout to 30 seconds, made it configurable through the `ipfs.stat_timeout` config value and return a 404 from the endpoint if the stat op times out nonetheless.
1 parent 2493813 commit f64dc9c

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/aleph/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ def get_defaults():
213213
# Timeout for pinning operations (seconds)
214214
"timeout": 60,
215215
},
216+
# Timeout for file stat requests (seconds)
217+
"stat_timeout": 30,
216218
},
217219
"rabbitmq": {
218220
# Hostname of the RabbitMQ service.

src/aleph/handlers/content/store.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ async def fetch_related_content(
115115
try:
116116
# The timeout of the aioipfs client does not seem to work, time out manually
117117
stats = await asyncio.wait_for(
118-
ipfs_client.files.stat(f"/ipfs/{item_hash}"), 5
118+
ipfs_client.files.stat(f"/ipfs/{item_hash}"),
119+
config.ipfs.stat_timeout.value,
119120
)
120121
except aioipfs.InvalidCIDError as e:
121122
raise UnknownHashError(

src/aleph/web/controllers/ipfs.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ async def ipfs_add_file(request: web.Request):
5353

5454
# IPFS add returns the cumulative size and not the real file size.
5555
# We need the real file size here.
56-
stats = await asyncio.wait_for(
57-
ipfs_service.ipfs_client.files.stat(f"/ipfs/{cid}"), 5
58-
)
59-
size = stats["Size"]
56+
try:
57+
stats = await asyncio.wait_for(
58+
ipfs_service.ipfs_client.files.stat(f"/ipfs/{cid}"),
59+
config.ipfs.stat_timeout.value,
60+
)
61+
size = stats["Size"]
62+
except TimeoutError:
63+
raise web.HTTPNotFound(reason="File not found on IPFS")
6064

6165
with session_factory() as session:
6266
upsert_file(

0 commit comments

Comments
 (0)