Skip to content
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

execinfra: correctly reset the eval.Context in InternalClose #89093

Merged
merged 1 commit into from
Oct 2, 2022

Conversation

yuzefovich
Copy link
Member

@yuzefovich yuzefovich commented Sep 30, 2022

This commit fixes the way we restore the eval.Context.Context in
ProcessorBase.InternalClose. Whenever a processor is started with
StartInternal, we derive a tracing span, and in order for all things
related to that processor (including the evaluation of the builtin
functions) to be correctly attributed to the tracing span of the processor,
we update the eval context with the new context. Most processors get
a fresh copy of the eval context via FlowCtx.NewEvalCtx, but a couple
(that are deemed to be "safe") do not do that (since they don't actually
evaluate any expressions) and, instead, reuse the "main" eval context.
Then, when the processor is closed, we want to reset the eval context to
its original form, and we had a bug here - we would use the context that
was passed into StartInternal rather than the "original" one, so we
could leak the context with an already finished tracing span outside.
This is now fixed.

This bug could occur for example when we have a FlowCoordinator (which
doesn't create a copy of the eval context) at the root of the main query
and then there are some postqueries - the eval context used by the
postqueries would refer to the context from the main query with
a finished tracing span. However, the bug was mitigated by explicitly
setting the context before running each postquery (due to an unrelated
old issue).

One could argue that since the processors modify the eval context, they
should always use a copy of the eval context. That would be unfortunate
since it would lead to increased allocations with the only modification
being this context-mangering business. The fix in this commit seems more
appropriate (both philosophically speaking and performance-wise).

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @msirek and @yuzefovich)


-- commits line 8 at r1:
[nit] be -> to be

This commit fixes the way we restore the `eval.Context.Context` in
`ProcessorBase.InternalClose`. Whenever a processor is started with
`StartInternal`, we derive a tracing span, and in order for all things
related to that processor (including the evaluation of the builtin
functions) to be correctly attributed to the tracing span of the processor,
we update the eval context with the new context. Most processors get
a fresh copy of the eval context via `FlowCtx.NewEvalCtx`, but a couple
(that are deemed to be "safe") do not do that (since they don't actually
evaluate any expressions) and, instead, reuse the "main" eval context.
Then, when the processor is closed, we want to reset the eval context to
its original form, and we had a bug here - we would use the context that
was passed into `StartInternal` rather than the "original" one, so we
could leak the context with an already finished tracing span outside.
This is now fixed.

This bug could occur for example when we have a `FlowCoordinator` (which
doesn't create a copy of the eval context) at the root of the main query
and then there are some postqueries - the eval context used by the
postqueries would refer to the context from the main query with
a finished tracing span. However, the bug was mitigated by explicitly
setting the context before running each postquery (due to an unrelated
old issue).

One could argue that since the processors modify the eval context, they
should always use a copy of the eval context. That would be unfortunate
since it would lead to increased allocations with the only modification
being this context-mangering business. The fix in this commit seems more
appropriate (both philosophically speaking and performance-wise).

Release note: None
Copy link
Member Author

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

TFTR!

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @DrewKimball and @msirek)


-- commits line 8 at r1:

Previously, DrewKimball (Drew Kimball) wrote…

[nit] be -> to be

Done.

@craig
Copy link
Contributor

craig bot commented Oct 2, 2022

Build succeeded:

@craig craig bot merged commit 6fcfd27 into cockroachdb:master Oct 2, 2022
@yuzefovich yuzefovich deleted the internal-close-ctx branch October 2, 2022 05:03
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.

3 participants