Skip to content

Commit

Permalink
Fix Socket closed build error (#567)
Browse files Browse the repository at this point in the history
# Description

If the build and push takes more than 10 minutes the connection gets
automatically closed by load balancer and the request fails with
UND_ERR_SOCKET error. In this case we just need to retry the request.
  • Loading branch information
jakubno authored Jan 30, 2025
2 parents e3e9d38 + bf1e1b1 commit 672622a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-pots-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2b/cli': patch
---

Fix socket closed error
43 changes: 31 additions & 12 deletions packages/cli/src/commands/template/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,39 @@ async function requestTemplateRebuild(
}

async function triggerTemplateBuild(templateID: string, buildID: string) {
const res = await client.api.POST(
'/templates/{templateID}/builds/{buildID}',
{
params: {
path: {
templateID,
buildID,
},
},
let res
const maxRetries = 3
for (let i = 0; i < maxRetries; i++) {
try {
res = await client.api.POST(
'/templates/{templateID}/builds/{buildID}',
{
params: {
path: {
templateID,
buildID,
},
},
}
)

break
} catch (e) {
// If the build and push takes more than 10 minutes the connection gets automatically closed by load balancer
// and the request fails with UND_ERR_SOCKET error. In this case we just need to retry the request.
if ((e instanceof TypeError) && (((e as TypeError).cause) as any)?.code !== 'UND_ERR_SOCKET') {
console.error(e)
console.log('Retrying...')
}
}
)
}

handleE2BRequestError(res.error, 'Error triggering template build')
return res.data
if (!res) {
throw new Error('Error triggering template build')
}

handleE2BRequestError(res?.error, 'Error triggering template build')
return res?.data
}

export const buildCommand = new commander.Command('build')
Expand Down

0 comments on commit 672622a

Please sign in to comment.