-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
Fix leakage of promises #497
Open
MahdiBM
wants to merge
4
commits into
vapor:main
Choose a base branch
from
MahdiBM:mmbm-fix-promise-leak
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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 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 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 |
---|---|---|
|
@@ -85,8 +85,8 @@ struct ConnectionStateMachine { | |
// Connection Actions | ||
|
||
// --- general actions | ||
case sendParseDescribeBindExecuteSync(PostgresQuery) | ||
case sendBindExecuteSync(PSQLExecuteStatement) | ||
case sendParseDescribeBindExecuteSync(PostgresQuery, promise: EventLoopPromise<Void>?) | ||
case sendBindExecuteSync(PSQLExecuteStatement, promise: EventLoopPromise<Void>?) | ||
case failQuery(EventLoopPromise<PSQLRowStream>, with: PSQLError, cleanupContext: CleanUpContext?) | ||
case succeedQuery(EventLoopPromise<PSQLRowStream>, with: QueryResult) | ||
|
||
|
@@ -97,12 +97,12 @@ struct ConnectionStateMachine { | |
case forwardStreamError(PSQLError, read: Bool, cleanupContext: CleanUpContext?) | ||
|
||
// Prepare statement actions | ||
case sendParseDescribeSync(name: String, query: String, bindingDataTypes: [PostgresDataType]) | ||
case sendParseDescribeSync(name: String, query: String, bindingDataTypes: [PostgresDataType], promise: EventLoopPromise<Void>?) | ||
case succeedPreparedStatementCreation(EventLoopPromise<RowDescription?>, with: RowDescription?) | ||
case failPreparedStatementCreation(EventLoopPromise<RowDescription?>, with: PSQLError, cleanupContext: CleanUpContext?) | ||
|
||
// Close actions | ||
case sendCloseSync(CloseTarget) | ||
case sendCloseSync(CloseTarget, promise: EventLoopPromise<Void>?) | ||
case succeedClose(CloseCommandContext) | ||
case failClose(CloseCommandContext, with: PSQLError, cleanupContext: CleanUpContext?) | ||
} | ||
|
@@ -234,7 +234,7 @@ struct ConnectionStateMachine { | |
} | ||
self.state = .sslNegotiated | ||
return .establishSSLConnection | ||
|
||
case .initialized, | ||
.sslNegotiated, | ||
.sslHandlerAdded, | ||
|
@@ -247,7 +247,7 @@ struct ConnectionStateMachine { | |
.closing, | ||
.closed: | ||
return self.closeConnectionAndCleanup(.unexpectedBackendMessage(.sslSupported)) | ||
|
||
case .modifying: | ||
preconditionFailure("Invalid state: \(self.state)") | ||
} | ||
|
@@ -583,14 +583,16 @@ struct ConnectionStateMachine { | |
} | ||
|
||
switch task { | ||
case .extendedQuery(let queryContext): | ||
case .extendedQuery(let queryContext, let writePromise): | ||
writePromise?.fail(psqlErrror) /// Use `cleanupContext` or not? | ||
switch queryContext.query { | ||
case .executeStatement(_, let promise), .unnamed(_, let promise): | ||
return .failQuery(promise, with: psqlErrror, cleanupContext: nil) | ||
case .prepareStatement(_, _, _, let promise): | ||
return .failPreparedStatementCreation(promise, with: psqlErrror, cleanupContext: nil) | ||
} | ||
case .closeCommand(let closeContext): | ||
case .closeCommand(let closeContext, let writePromise): | ||
writePromise?.fail(psqlErrror) /// Use `cleanupContext` or not? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment |
||
return .failClose(closeContext, with: psqlErrror, cleanupContext: nil) | ||
} | ||
} | ||
|
@@ -934,17 +936,17 @@ struct ConnectionStateMachine { | |
} | ||
|
||
switch task { | ||
case .extendedQuery(let queryContext): | ||
case .extendedQuery(let queryContext, let promise): | ||
self.state = .modifying // avoid CoW | ||
var extendedQuery = ExtendedQueryStateMachine(queryContext: queryContext) | ||
let action = extendedQuery.start() | ||
let action = extendedQuery.start(promise) | ||
self.state = .extendedQuery(extendedQuery, connectionContext) | ||
return self.modify(with: action) | ||
|
||
case .closeCommand(let closeContext): | ||
case .closeCommand(let closeContext, let promise): | ||
self.state = .modifying // avoid CoW | ||
var closeStateMachine = CloseStateMachine(closeContext: closeContext) | ||
let action = closeStateMachine.start() | ||
let action = closeStateMachine.start(promise) | ||
self.state = .closeCommand(closeStateMachine, connectionContext) | ||
return self.modify(with: action) | ||
} | ||
|
@@ -1031,10 +1033,10 @@ extension ConnectionStateMachine { | |
extension ConnectionStateMachine { | ||
mutating func modify(with action: ExtendedQueryStateMachine.Action) -> ConnectionStateMachine.ConnectionAction { | ||
switch action { | ||
case .sendParseDescribeBindExecuteSync(let query): | ||
return .sendParseDescribeBindExecuteSync(query) | ||
case .sendBindExecuteSync(let executeStatement): | ||
return .sendBindExecuteSync(executeStatement) | ||
case .sendParseDescribeBindExecuteSync(let query, let promise): | ||
return .sendParseDescribeBindExecuteSync(query, promise: promise) | ||
case .sendBindExecuteSync(let executeStatement, let promise): | ||
return .sendBindExecuteSync(executeStatement, promise: promise) | ||
case .failQuery(let requestContext, with: let error): | ||
let cleanupContext = self.setErrorAndCreateCleanupContextIfNeeded(error) | ||
return .failQuery(requestContext, with: error, cleanupContext: cleanupContext) | ||
|
@@ -1057,8 +1059,8 @@ extension ConnectionStateMachine { | |
return .read | ||
case .wait: | ||
return .wait | ||
case .sendParseDescribeSync(name: let name, query: let query, bindingDataTypes: let bindingDataTypes): | ||
return .sendParseDescribeSync(name: name, query: query, bindingDataTypes: bindingDataTypes) | ||
case .sendParseDescribeSync(name: let name, query: let query, bindingDataTypes: let bindingDataTypes, let promise): | ||
return .sendParseDescribeSync(name: name, query: query, bindingDataTypes: bindingDataTypes, promise: promise) | ||
case .succeedPreparedStatementCreation(let promise, with: let rowDescription): | ||
return .succeedPreparedStatementCreation(promise, with: rowDescription) | ||
case .failPreparedStatementCreation(let promise, with: let error): | ||
|
@@ -1094,8 +1096,8 @@ extension ConnectionStateMachine { | |
extension ConnectionStateMachine { | ||
mutating func modify(with action: CloseStateMachine.Action) -> ConnectionStateMachine.ConnectionAction { | ||
switch action { | ||
case .sendCloseSync(let sendClose): | ||
return .sendCloseSync(sendClose) | ||
case .sendCloseSync(let sendClose, let promise): | ||
return .sendCloseSync(sendClose, promise: promise) | ||
case .succeedClose(let closeContext): | ||
return .succeedClose(closeContext) | ||
case .failClose(let closeContext, with: let error): | ||
|
This file contains 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 |
---|---|---|
|
@@ -26,10 +26,10 @@ struct ExtendedQueryStateMachine { | |
} | ||
|
||
enum Action { | ||
case sendParseDescribeBindExecuteSync(PostgresQuery) | ||
case sendParseDescribeSync(name: String, query: String, bindingDataTypes: [PostgresDataType]) | ||
case sendBindExecuteSync(PSQLExecuteStatement) | ||
case sendParseDescribeBindExecuteSync(PostgresQuery, promise: EventLoopPromise<Void>?) | ||
case sendParseDescribeSync(name: String, query: String, bindingDataTypes: [PostgresDataType], promise: EventLoopPromise<Void>?) | ||
case sendBindExecuteSync(PSQLExecuteStatement, promise: EventLoopPromise<Void>?) | ||
|
||
// --- general actions | ||
case failQuery(EventLoopPromise<PSQLRowStream>, with: PSQLError) | ||
case succeedQuery(EventLoopPromise<PSQLRowStream>, with: QueryResult) | ||
|
@@ -57,7 +57,7 @@ struct ExtendedQueryStateMachine { | |
self.state = .initialized(queryContext) | ||
} | ||
|
||
mutating func start() -> Action { | ||
mutating func start(_ promise: EventLoopPromise<Void>?) -> Action { | ||
guard case .initialized(let queryContext) = self.state else { | ||
preconditionFailure("Start should only be called, if the query has been initialized") | ||
} | ||
|
@@ -66,7 +66,7 @@ struct ExtendedQueryStateMachine { | |
case .unnamed(let query, _): | ||
return self.avoidingStateMachineCoW { state -> Action in | ||
state = .messagesSent(queryContext) | ||
return .sendParseDescribeBindExecuteSync(query) | ||
return .sendParseDescribeBindExecuteSync(query, promise: promise) | ||
} | ||
|
||
case .executeStatement(let prepared, _): | ||
|
@@ -77,13 +77,14 @@ struct ExtendedQueryStateMachine { | |
case .none: | ||
state = .noDataMessageReceived(queryContext) | ||
} | ||
return .sendBindExecuteSync(prepared) | ||
return .sendBindExecuteSync(prepared, promise: promise) | ||
} | ||
|
||
/// Not my code, but this is ignoring the last argument which is a promise? is that fine? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment |
||
case .prepareStatement(let name, let query, let bindingDataTypes, _): | ||
return self.avoidingStateMachineCoW { state -> Action in | ||
state = .messagesSent(queryContext) | ||
return .sendParseDescribeSync(name: name, query: query, bindingDataTypes: bindingDataTypes) | ||
return .sendParseDescribeSync(name: name, query: query, bindingDataTypes: bindingDataTypes, promise: promise) | ||
} | ||
} | ||
} | ||
|
This file contains 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment