Skip to content

Conversation

SyedaAnshrahGillani
Copy link

Summary

This PR addresses a critical resource management and performance issue in the SimStudioClient by implementing proper request abortion for timed-out fetch calls.


Problem

Previously, the executeWorkflow method used Promise.race to handle timeouts. While this approach rejected the timeout promise, the underlying fetch request continued running in the background, leading to:

  • Resource Leaks: Network connections remained open, increasing memory consumption.
  • Performance Degradation: Continued background activity added unnecessary load.
  • Unpredictable Behavior: Late server responses to already timed-out requests could lead to inconsistent client state.

Solution

The method is refactored to use the AbortController API for more robust timeout handling:

  • A new AbortController is created for each fetch request.
  • A setTimeout is used to trigger abortController.abort() after the configured timeout period.
  • The controller’s signal is passed to the fetch options to enable cancellation.
  • clearTimeout is called once the request completes, preventing stale timeouts.
  • Error handling is enhanced to catch AbortError and provide clear messaging for timeout events.

Benefits

  • Improved Resource Management — Cleans up network and memory resources on timeout.
  • Enhanced Performance — Prevents wasteful processing by halting stalled requests.
  • Predictable Behavior — Ensures client-side state remains consistent by terminating timed-out calls.
  • Best Practice Compliance — Aligns with modern JavaScript standards for request cancellation.

@vercel vercel bot temporarily deployed to Preview – docs July 10, 2025 21:56 Inactive
Copy link

vercel bot commented Jul 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
docs ⬜️ Skipped (Inspect) Jul 10, 2025 9:56pm

Copy link

vercel bot commented Jul 10, 2025

@SyedaAnshrahGillani is attempting to deploy a commit to the Sim Studio Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Enhanced request timeout handling in SimStudioClient by implementing AbortController for fetch requests, ensuring proper cleanup of network resources and preventing memory leaks.

  • Replaced Promise.race implementation with AbortController in packages/ts-sdk/src/index.ts for better request cancellation
  • Added signal propagation to fetch options for proper request termination
  • Implemented clearTimeout cleanup to prevent stale timeout handlers
  • Tests confirm proper timeout handling with 408 status codes and error messages

1 file reviewed, 2 comments
Edit PR Review Bot Settings | Greptile

Comment on lines +69 to 79
try {
const fetchPromise = fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': this.apiKey,
},
body: JSON.stringify(input || {}),
signal: abortController.signal, // Attach the abort signal
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Remove nested try block - already inside a try block. Move catch handler up to avoid nested error handling.

Suggested change
try {
const fetchPromise = fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': this.apiKey,
},
body: JSON.stringify(input || {}),
signal: abortController.signal, // Attach the abort signal
})
const fetchPromise = fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': this.apiKey,
},
body: JSON.stringify(input || {}),
signal: abortController.signal, // Attach the abort signal
})

Comment on lines 95 to 97
} catch (error: any) {
if (error instanceof SimStudioError) {
throw error
}
clearTimeout(timeoutId) // Ensure timeout is cleared on error too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: clearTimeout(timeoutId) is indented incorrectly, making it unreachable for some error paths. Move to same level as catch block.

Suggested change
} catch (error: any) {
if (error instanceof SimStudioError) {
throw error
}
clearTimeout(timeoutId) // Ensure timeout is cleared on error too
} catch (error: any) {
clearTimeout(timeoutId) // Ensure timeout is cleared on error too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant