fix(response): conflict when handler completed and concurrent map writes #55
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.
Resolve the #15
fatal error: concurrent map writes
issueCause
In the current implementation, when a timeout occurs,
c.Writer
is replaced with&gin.responseWriter
and writing to the Body is performed by theTimeout.response
handler.Subsequently, additional writes to the
Body
are performed by the handler being executed in a goroutine.This results in writes being performed to the
Body
in a duplicate manner.Countermeasure
Before replacing
c.Writer
, use*gin.Context.Copy()
to duplicate the context. This ensures thatc.Writer
is only set to&gin.responseWriter
whenTimeout.response
handler is executed.Verification
Execute the following verification code.
Request 20 times consecutively using the following command.
Before the fix, there were instances of duplicated
Body
content. Additionally, the application occasionally encountered afatal error: concurrent map writes
and would stop.After the fix, there is no duplication of the
Body
, and the application does not encounter afatal error: concurrent map writes
that would cause it to stop.