-
Notifications
You must be signed in to change notification settings - Fork 79
Removes two entry points for memory leaks #182
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
Merged
NeedleInAJayStack
merged 2 commits into
GraphQLSwift:main
from
jwsinner:fix/memory-leak
Jun 25, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import Testing | ||
|
|
||
| @testable import GraphQL | ||
|
|
||
| // Holds a weak reference to a Token so we can observe when ARC frees it. | ||
| private final class WeakTokenBox { | ||
| weak var token: Token? | ||
| } | ||
|
|
||
| @Suite struct GraphQLMemoryRetentionTests { | ||
| private func makeSchema() throws -> GraphQLSchema { | ||
| try GraphQLSchema( | ||
| query: GraphQLObjectType( | ||
| name: "Query", | ||
| fields: ["hello": GraphQLField(type: GraphQLString)] | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| // Regression test for Token.prev being a strong reference. | ||
| // | ||
| // Adjacent tokens in the linked list formed a mutual retain cycle: | ||
| // SOF.next → T1 and T1.prev → SOF. When the Document went out of scope, | ||
| // neither token could be freed. Making prev `weak` breaks the cycle. | ||
| @Test func tokenChainIsReleasedAfterDocumentGoesOutOfScope() throws { | ||
| let box = WeakTokenBox() | ||
|
|
||
| func parseAndCapture() throws { | ||
| let document = try parse(source: "{ hello }") | ||
| // startToken is SOF (no prev). Capture the next token, which | ||
| // has a .prev back to SOF — the exact edge the fix targets. | ||
| box.token = document.loc?.startToken.next | ||
| #expect(box.token != nil) | ||
| } | ||
|
|
||
| try parseAndCapture() | ||
| #expect(box.token == nil) | ||
| } | ||
|
|
||
| // Regression test for ValidationContext.init capturing `self` in a closure. | ||
| // | ||
| // The onError closure stored on ASTValidationContext captured ValidationContext | ||
| // strongly, creating a cycle that prevented the context (and the Document it | ||
| // holds) from ever being freed. Overriding report(error:) instead eliminates | ||
| // the closure and the cycle. | ||
| // | ||
| // SOF (startToken) has no .prev, so it has no token-chain cycle. If it is | ||
| // retained after this scope it must be because ValidationContext leaked and | ||
| // is still holding a copy of the Document. | ||
| @Test func validationContextReleasesDocumentAfterValidation() throws { | ||
| let schema = try makeSchema() | ||
| let box = WeakTokenBox() | ||
|
|
||
| func validateAndCapture() throws { | ||
| let document = try parse(source: "{ hello }") | ||
| box.token = document.loc?.startToken // SOF — no prev, no token cycle | ||
| let errors = validate(schema: schema, ast: document) | ||
| #expect(errors.isEmpty) | ||
| #expect(box.token != nil) | ||
| } | ||
|
|
||
| try validateAndCapture() | ||
| #expect(box.token == nil) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.