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

[pythonic resources][fix] fix setup/teardown behavior w/ nested pythonic resources #19852

Merged
merged 2 commits into from
Feb 16, 2024

Conversation

benpankow
Copy link
Member

Summary

Addresses #19277 and #14112.

When nested in a non-context-manager resource (e.g. no teardown_after_execution or yield_for_execution override), context manager resources were not properly opened before step execution and closed afterwards, meaning their teardown logic was invoked immediately.

This PR treats all resources with nested resources (which may or may not be context managers) as context managers as a safeguard to prevent this from happening.

Test Plan

Unit tests of the nested-yield and nested-teardown case, both which previously failed.

@benpankow
Copy link
Member Author

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@@ -399,6 +399,7 @@ def _is_cm_resource_cls(cls: Type["ConfigurableResourceFactory"]) -> bool:
return (
cls.yield_for_execution != ConfigurableResourceFactory.yield_for_execution
or cls.teardown_after_execution != ConfigurableResourceFactory.teardown_after_execution
or len(_get_resource_param_fields(cls)) > 0
Copy link
Member

@alangenfeld alangenfeld Feb 15, 2024

Choose a reason for hiding this comment

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

does this equate to "if there are any resources as params, assume they are context managers"? Is that the case because they always are in the base class?

edit: not sure my question actually makes sense but i think it points towards putting a comment here explaining whats going on

Copy link
Member Author

@benpankow benpankow Feb 16, 2024

Choose a reason for hiding this comment

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

It basically equates to "if this resource has any other nested resources, let's assume that they're context managers, and treat this resource as a context manager"

This boolean affects a few things:

  • The underlying resource function for the ResourceDefinition created from this ConfigurableResource - it's a CM function if True, not a CM if False (the source of the bug this PR addresses). This is largely opaque to the user, so biasing towards True is low risk.

  • A few utility methods, like from_resource_context - this method is typically used for migrations from function-style resources which construct the Pythonic-style resource. These for the most part shouldn't use nesting:

    class FancyDbResource(ConfigurableResource):
        conn_string: str
    
    @resource(config_schema=FancyDbResource.to_config_schema())
    def fancy_db_resource(context: InitResourceContext) -> FancyDbResource:
        return FancyDbResource.from_resource_context(context)

Copy link
Member

Choose a reason for hiding this comment

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

got it - think its worth a comment

@@ -399,6 +399,7 @@ def _is_cm_resource_cls(cls: Type["ConfigurableResourceFactory"]) -> bool:
return (
cls.yield_for_execution != ConfigurableResourceFactory.yield_for_execution
or cls.teardown_after_execution != ConfigurableResourceFactory.teardown_after_execution
or len(_get_resource_param_fields(cls)) > 0
Copy link
Member

Choose a reason for hiding this comment

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

got it - think its worth a comment

@benpankow benpankow force-pushed the benpankow/fix-setup-teardown-nesting-cm branch from 51f761f to a9d4637 Compare February 16, 2024 22:12
Copy link

Deploy preview for dagster-university ready!

✅ Preview
https://dagster-university-4zucrtljg-elementl.vercel.app
https://benpankow-fix-setup-teardown-nesting-cm.dagster-university.dagster-docs.io

Built with commit a9d4637.
This pull request is being automatically deployed with vercel-action

Copy link

Deploy preview for dagit-storybook ready!

✅ Preview
https://dagit-storybook-3xhl5foy5-elementl.vercel.app
https://benpankow-fix-setup-teardown-nesting-cm.components-storybook.dagster-docs.io

Built with commit a9d4637.
This pull request is being automatically deployed with vercel-action

@benpankow benpankow merged commit 472b687 into master Feb 16, 2024
2 of 3 checks passed
@benpankow benpankow deleted the benpankow/fix-setup-teardown-nesting-cm branch February 16, 2024 23:04
@C0DK
Copy link
Contributor

C0DK commented Feb 19, 2024

Thank you ❤️

PedramNavid pushed a commit that referenced this pull request Mar 28, 2024
…nic resources (#19852)

## Summary

Addresses #19277 and #14112.

When nested in a non-context-manager resource (e.g. no
`teardown_after_execution` or `yield_for_execution` override), context
manager resources were not properly opened before step execution and
closed afterwards, meaning their teardown logic was invoked immediately.

This PR treats all resources with nested resources (which may or may not
be context managers) as context managers as a safeguard to prevent this
from happening.

## Test Plan

Unit tests of the nested-yield and nested-teardown case, both which
previously failed.
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