Skip to content

Simplify and stabilise AbortSignal.any ponyfill #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed

Conversation

enisdenjo
Copy link
Member

@enisdenjo enisdenjo commented Feb 4, 2025

Closes #303
Ref GW-30, ref GW-140

Fix is confirmed through local load tests with constant 10 VUs over 3mins:

  • Before the fix, the mem consumption would grow consistently from ~400mb idle to ~3GB and the resources would not get freed after the loadtest ends
  • After the fix, the mem consumption would grow from ~400mb idle to ~1.5GB at peak and the resources would get freed up after the loadtest ends returning the consumption back to ~400mb idle

TODO

  • Develop and perform loadtests in the CI

@theguild-bot
Copy link
Collaborator

theguild-bot commented Feb 4, 2025

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-hive/gateway-abort-signal-any 1.0.0-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-tools/executor-http 1.2.6-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-tools/federation 3.1.1-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-mesh/fusion-runtime 0.10.32-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-hive/gateway 1.9.4-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-mesh/plugin-opentelemetry 1.3.40-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-mesh/plugin-prometheus 1.3.28-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-hive/gateway-runtime 1.4.12-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-mesh/transport-common 0.7.28-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-mesh/transport-http 0.6.32-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-mesh/transport-http-callback 0.5.19-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎
@graphql-mesh/transport-ws 1.0.2-alpha-35ae4466252ee2216728847c3c982221daa617af npm ↗︎ unpkg ↗︎

Copy link
Contributor

coderabbitai bot commented Feb 4, 2025

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Enhanced cancellation handling now leverages the native abort signal for a more reliable and streamlined experience.
  • Bug Fixes

    • Addressed issues causing potential memory leaks during abort events, improving overall stability.
  • Chores

    • Streamlined package dependencies and configurations for a cleaner, more efficient setup.

Walkthrough

This pull request updates the handling of abort signals by introducing a new dependency (@graphql-hive/gateway-abort-signal-any) with a major version bump. The implementation of the abortSignalAny function has been refactored to return the native AbortSignal while removing custom types and signal management logic. Additionally, the upstream cancellation plugin has been simplified to directly use the new abort signal helper. A patch in the runtime package improves the ponyfill for AbortSignal.any, streamlining event listener cleanup to mitigate potential memory leaks.

Changes

File(s) Change Summary
.changeset/famous-mangos-rest.md, .changeset/little-plums-perform.md, .changeset/long-beds-dream.md Introduce/update dependency @graphql-hive/gateway-abort-signal-any with a major version update; update abortSignalAny to return native AbortSignal and remove deprecated types.
.changeset/real-seals-taste.md Patch update for @graphql-hive/gateway-runtime to improve the AbortSignal.any ponyfill and mitigate potential memory leaks.
packages/abort-signal-any/src/index.ts Refactor abortSignalAny: remove custom type AbortSignalFromAny, simplify signal combination logic, and streamline event listener cleanup using native methods.
packages/runtime/src/plugins/useUpstreamCancel.ts Simplify abort signal handling by removing type-checks for custom signals and directly assigning the result of abortSignalAny to the appropriate signal properties.
packages/abort-signal-any/package.json, packages/abort-signal-any/src/index.ts Remove multiple dependencies and configurations to streamline package setup.

Assessment against linked issues

Objective Addressed Explanation
Prevent memory leak when setting options.timeout per subgraph (#303, GW-30) The improvements in abort signal handling and event listener cleanup address potential memory leaks.

Possibly related PRs

  • Upcoming Release Changes #583: The changes in the main PR regarding the abortSignalAny function and its dependency updates are related to the modifications in the retrieved PR that also involve updates to the @graphql-hive/gateway-abort-signal-any package, specifically focusing on the abortSignalAny function's return type and functionality.

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/abort-signal-any/src/index.ts (1)

17-22: Consider adding a guard to avoid multiple abort calls.

If multiple signals abort in quick succession, the abort function may be triggered more than once. Adding a check—for instance, if ctrl.signal.aborted—could prevent redundant calls to ctrl.abort().

.changeset/little-plums-perform.md (1)

4-8: Refine Explanation and Improve Readability
The note now indicates that abortSignalAny returns the native AbortSignal instead of a custom AbortSignalFromAny. The intent is clear; however, the phrasing in the sentence could be improved for clarity and grammatical correctness. Consider rephrasing as follows:

-Instead of the custom `AbortSignalFromAny`. There is no need for the complication of adding signals to existing sets, chaining signals enough as well as simpler.
+Instead of using a custom `AbortSignalFromAny`, the function now returns the native `AbortSignal` directly. This change simplifies signal management by eliminating the need to add signals to existing sets and chain them unnecessarily.

This revision clarifies the benefits and removes ambiguity.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a2f31f4 and d6fbffa.

📒 Files selected for processing (6)
  • .changeset/famous-mangos-rest.md (1 hunks)
  • .changeset/little-plums-perform.md (1 hunks)
  • .changeset/long-beds-dream.md (1 hunks)
  • .changeset/real-seals-taste.md (1 hunks)
  • packages/abort-signal-any/src/index.ts (1 hunks)
  • packages/runtime/src/plugins/useUpstreamCancel.ts (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .changeset/long-beds-dream.md
🧰 Additional context used
📓 Path-based instructions (2)
packages/runtime/src/plugins/useUpstreamCancel.ts (2)

Pattern packages/**: In this directory we keep all packages relevant to the gateway.

In most cases, when changes are made to the source code / dependencies in these directories, we also require to have a corresponding changeset file, created using yarn changeset with a full description on the changes with an example.
Also, when new features are added the packages, the changeset file should mention minor semver change. When a bug is fixed, patch needs to be used. The major bump is used for a PR that has breaking changes.


Pattern **: For all PRs, we would like to verify that a Linear issue is linked. Check if there's a mention for "GW-*" in the PR description, and the us know when it's missing.

In most cases, when a PR adds a new functionality, we would like to make sure it has documentation in the corresponding console project.
Use the global knowledge feature to search for PRs in graphql-hive/console repository that adds documentation. Suggest to create a follow up issue to add the missing documentation.

packages/abort-signal-any/src/index.ts (2)

Pattern packages/**: In this directory we keep all packages relevant to the gateway.

In most cases, when changes are made to the source code / dependencies in these directories, we also require to have a corresponding changeset file, created using yarn changeset with a full description on the changes with an example.
Also, when new features are added the packages, the changeset file should mention minor semver change. When a bug is fixed, patch needs to be used. The major bump is used for a PR that has breaking changes.


Pattern **: For all PRs, we would like to verify that a Linear issue is linked. Check if there's a mention for "GW-*" in the PR description, and the us know when it's missing.

In most cases, when a PR adds a new functionality, we would like to make sure it has documentation in the corresponding console project.
Use the global knowledge feature to search for PRs in graphql-hive/console repository that adds documentation. Suggest to create a follow up issue to add the missing documentation.

⏰ Context from checks skipped due to timeout of 90000ms (24)
  • GitHub Check: Unit / Bun
  • GitHub Check: Unit / Node v20
  • GitHub Check: E2E / Bun Docker on Ubuntu
  • GitHub Check: Unit / Node v18
  • GitHub Check: E2E / Bun on Ubuntu
  • GitHub Check: E2E / Node Binary on Windows
  • GitHub Check: E2E / Node Binary on Ubuntu
  • GitHub Check: E2E / Node Docker on Ubuntu
  • GitHub Check: Benchmark / bun / 1000 items
  • GitHub Check: Leaks / Node v22
  • GitHub Check: Snapshot / snapshot
  • GitHub Check: Benchmark / bun / 100 items
  • GitHub Check: Build
  • GitHub Check: E2E / Node 22 on Ubuntu
  • GitHub Check: Benchmark / bun / 10 items
  • GitHub Check: Bundle
  • GitHub Check: Leaks / Node v20
  • GitHub Check: Benchmark / node / 1000 items
  • GitHub Check: Leaks / Node v18
  • GitHub Check: Benchmark / node / 100 items
  • GitHub Check: E2E / Node 20 on Ubuntu
  • GitHub Check: Benchmark / node / 10 items
  • GitHub Check: E2E / Node 18 on Ubuntu
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (7)
packages/abort-signal-any/src/index.ts (1)

1-2: Handle empty iterable scenarios to avoid unintended never-aborting signals.

If the iterable is empty, this returns a never-aborting signal. Verify whether this behavior is desired, or consider short-circuiting to avoid unexpected hangs in downstream code.

packages/runtime/src/plugins/useUpstreamCancel.ts (2)

21-24: Confirm the desired behavior with an empty signals array.

If no signals exist, abortSignalAny([]) returns a never-aborting signal. Please verify this is the intended logic when no signals are present.


34-37: Maintain consistency for empty signals.

Similarly, if there are no signals here, abortSignalAny([]) yields a never-aborting signal. Ensure this aligns with your intended upstream cancellation flow.

.changeset/real-seals-taste.md (1)

2-2: Patch version aligns with the bug-fix guidelines.

Using a patch increment for a memory-leak fix follows the prescribed semver for bug fixes. Good job maintaining consistent versioning.

.changeset/famous-mangos-rest.md (2)

1-3: Dependency Version Update Noted
The changeset correctly indicates that the dependency @graphql-hive/gateway-abort-signal-any is now bumped to a major version. This level of specificity is useful to inform downstream consumers of potential breaking changes.


4-6: Clear Description of abortSignalAny Behavior
The note explains that abortSignalAny will use AbortSignal.any when available and fall back to AbortController for compatibility. This concise explanation is helpful; ensure that any related documentation elsewhere in the project is also updated to reflect this behavior.

.changeset/little-plums-perform.md (1)

1-3: Dependency Version Update Confirmation
Similar to the other changeset, this file specifies the major version update for @graphql-hive/gateway-abort-signal-any. Consistency in documenting this dependency update across changesets is appreciated.

@theguild-bot
Copy link
Collaborator

theguild-bot commented Feb 4, 2025

🚀 Snapshot Release (Binary for Linux-X64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Collaborator

theguild-bot commented Feb 4, 2025

🚀 Snapshot Release (Binary for macOS-ARM64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Collaborator

theguild-bot commented Feb 4, 2025

🚀 Snapshot Release (Bun Docker Image)

The latest changes of this PR are available as image on GitHub Container Registry (based on the declared changesets):

ghcr.io/graphql-hive/gateway:1.9.4-alpha-35ae4466252ee2216728847c3c982221daa617af-bun

@theguild-bot
Copy link
Collaborator

theguild-bot commented Feb 4, 2025

🚀 Snapshot Release (Node Docker Image)

The latest changes of this PR are available as image on GitHub Container Registry (based on the declared changesets):

ghcr.io/graphql-hive/gateway:1.9.4-alpha-35ae4466252ee2216728847c3c982221daa617af

@theguild-bot
Copy link
Collaborator

theguild-bot commented Feb 4, 2025

🚀 Snapshot Release (Binary for macOS-X64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

Copy link
Member

@ardatan ardatan left a comment

Choose a reason for hiding this comment

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

CI seems failing. Should we keep this draft until we figure it out?

Copy link
Member Author

Yes, I am on fixing the tests.

@enisdenjo enisdenjo marked this pull request as draft February 4, 2025 15:09
@theguild-bot
Copy link
Collaborator

theguild-bot commented Feb 4, 2025

🚀 Snapshot Release (Binary for Windows-X64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Collaborator

theguild-bot commented Feb 4, 2025

🚀 Snapshot Release (Binary for Linux-ARM64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/abort-signal-any/src/index.ts (1)

10-13: Convert TODO into proper documentation.

The TODO comment contains valuable information about compatibility with whatwg-node signals. Consider converting it into a proper JSDoc comment to better document this limitation.

-  // TODO: we cant use the native abortsignal.any because it doesnt work with signals coming from whatwg-node (ServerAdapterRequestAbortSignal)
-  // if ('any' in AbortSignal) {
-  //   return AbortSignal.any(signals);
-  // }
+  /**
+   * Note: We can't use the native AbortSignal.any because it doesn't work with
+   * signals coming from whatwg-node (ServerAdapterRequestAbortSignal).
+   * The following native implementation is kept for reference:
+   * ```ts
+   * if ('any' in AbortSignal) {
+   *   return AbortSignal.any(signals);
+   * }
+   * ```
+   */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d6fbffa and 35ae446.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (8)
  • .changeset/@graphql-hive_gateway-abort-signal-any-593-dependencies.md (1 hunks)
  • .changeset/famous-mangos-rest.md (1 hunks)
  • .changeset/little-plums-perform.md (1 hunks)
  • .changeset/long-beds-dream.md (1 hunks)
  • .changeset/real-seals-taste.md (1 hunks)
  • packages/abort-signal-any/package.json (0 hunks)
  • packages/abort-signal-any/src/index.ts (1 hunks)
  • packages/runtime/src/plugins/useUpstreamCancel.ts (3 hunks)
💤 Files with no reviewable changes (1)
  • packages/abort-signal-any/package.json
✅ Files skipped from review due to trivial changes (1)
  • .changeset/@graphql-hive_gateway-abort-signal-any-593-dependencies.md
🚧 Files skipped from review as they are similar to previous changes (5)
  • .changeset/long-beds-dream.md
  • .changeset/real-seals-taste.md
  • .changeset/little-plums-perform.md
  • .changeset/famous-mangos-rest.md
  • packages/runtime/src/plugins/useUpstreamCancel.ts
🧰 Additional context used
📓 Path-based instructions (1)
packages/abort-signal-any/src/index.ts (2)

Pattern packages/**: In this directory we keep all packages relevant to the gateway.

In most cases, when changes are made to the source code / dependencies in these directories, we also require to have a corresponding changeset file, created using yarn changeset with a full description on the changes with an example.
Also, when new features are added the packages, the changeset file should mention minor semver change. When a bug is fixed, patch needs to be used. The major bump is used for a PR that has breaking changes.


Pattern **: For all PRs, we would like to verify that a Linear issue is linked. Check if there's a mention for "GW-*" in the PR description, and the us know when it's missing.

In most cases, when a PR adds a new functionality, we would like to make sure it has documentation in the corresponding console project.
Use the global knowledge feature to search for PRs in graphql-hive/console repository that adds documentation. Suggest to create a follow up issue to add the missing documentation.

⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: Benchmark / bun / 1000 items
  • GitHub Check: Benchmark / bun / 100 items
  • GitHub Check: Bundle
  • GitHub Check: Benchmark / bun / 10 items
  • GitHub Check: Snapshot / snapshot
  • GitHub Check: Benchmark / node / 1000 items
  • GitHub Check: Build
  • GitHub Check: Benchmark / node / 100 items
  • GitHub Check: Benchmark / node / 10 items
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (3)
packages/abort-signal-any/src/index.ts (3)

1-2: LGTM! Clean and efficient initialization.

The parameter name change to iterable better reflects its type, and the upfront array conversion is necessary for multiple iterations.


4-8: LGTM! Efficient early abort check.

Good optimization to return an already aborted signal without setting up event listeners.


15-28: LGTM! Clean implementation with proper cleanup.

The implementation correctly chains signal abortion and includes proper cleanup of event listeners, which aligns with the PR's goal of improving memory consumption.

Let's verify the cleanup implementation with a test script:

✅ Verification successful

Cleanup logic in abort-signal-any is correctly implemented.

  • The event listener for the 'abort' event is added and properly removed on abort, preventing potential memory leaks.
  • The shell script confirms that this pattern matches similar implementations in the codebase, ensuring consistency.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential memory leaks in event listener cleanup

# Search for similar patterns in the codebase to ensure consistent cleanup
rg -A 5 'addEventListener.*abort' --type ts

Length of output: 709

@ardatan ardatan marked this pull request as draft February 5, 2025 10:26
@ardatan ardatan closed this in #598 Feb 5, 2025
@ardatan ardatan deleted the abort-sig-any branch March 29, 2025 12:42
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.

Memory Leak when setting options.timeout per subgraph in transportEntries
4 participants