Skip to content

Conversation

@tillahoffmann
Copy link
Contributor

@tillahoffmann tillahoffmann commented Jan 6, 2026

This follows up on #1069 to call close on the underlying data source if it implements a close method. This ensures resources are cleaned up properly.

For multi-threaded prefetch, the close method is called once per thread and on the main thread to clean up thread-local resources. This means that close should be safe to call multiple times which aligns with Python standard practice, e.g., closing streams, database connections, and sockets in the standard library have this behavior.


The docs mention that Grain tries to use data sources as context managers if they implement the protocol, but that behavior is not currently implemented (cf. #936). The code provides the utility function use_context_if_available, but it is not used.

@iindyk, given our discussion in #1069, I've used close rather than context managers to clean up resources. This achieves the clean up, but does not address the doc discrepancy in #936. I can add the context manager implementation but wanted to discuss first given the preference for close over context managers in #1069.


📚 Documentation preview 📚: https://google-grain--1185.org.readthedocs.build/

@google-cla
Copy link

google-cla bot commented Jan 6, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

This change ensures that when `DatasetIterator.close()` is called, the
cleanup propagates all the way down to the underlying data source,
allowing proper resource cleanup (e.g., closing file handles, database
connections, or thread-local resources).

Changes:
- Add `close()` to `MapDataset` that propagates to parent datasets
- Add `close()` to `SourceMapDataset` that calls the data source's
  `close()` method if available
- Update `PrefetchDatasetIterator.close()` to call `_map_parent.close()`
- Update `GetElementProducerFn.__call__` to call `it.close()` in finally
  block for multiprocessing cleanup
- Add tests for close propagation
@tillahoffmann tillahoffmann force-pushed the datasource-close-propagation branch from 97a22ce to efd84d6 Compare January 6, 2026 16:49
@iindyk
Copy link
Collaborator

iindyk commented Jan 9, 2026

thanks for the change Till.

When do we expect MapDataset.close to be called? MapDataset (and the sources) are created once per pipeline, whereas the iterator can be created multiple times (e.g. this is common for evaluation). So, we cannot close the source when we close the iterator because then the user will not be able to create an iterator again, right?

so in order to actually close the source the user will have to manually call close on the source itself, right? not on the MapDataset that contains

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.

2 participants