YongGoose opened a new pull request, #1049:
URL: https://github.com/apache/burr/pull/1049

   `MongoDBBasePersister.cleanup()` and `__exit__` call 
`self.connection.close()`, but the persister keeps its `MongoClient` in 
`self.client` and has no `connection` attribute. So both ways of closing a 
persister that [the persistence 
docs](https://burr.apache.org/concepts/state-persistence/) recommend raise 
`AttributeError` and leave the client open:
   
   ```python
   with MongoDBBasePersister.from_values(uri="mongodb://localhost:27017") as 
persister:
       ...  # build and run an app
   # AttributeError: 'MongoDBBasePersister' object has no attribute 
'connection'. Did you mean: 'collection'?
   
   persister = MongoDBBasePersister.from_values(uri="mongodb://localhost:27017")
   try:
       ...
   finally:
       persister.cleanup()  # same AttributeError
   ```
   
   The deprecated `b_mongodb.MongoDBBasePersister` and 
`b_mongodb.MongoDBPersister` inherit both methods, so they have the same 
problem.
   
   ## Changes
   
   - `cleanup()` and `__exit__` now call `self.client.close()`, which `__del__` 
already does.
   - Two integration tests in 
`tests/integrations/persisters/test_b_mongodb.py`: after `cleanup()`, and after 
leaving a `with` block, the persister's client is closed (pymongo raises 
`InvalidOperation` on the next `load()`).
   
   ## How I tested this
   
   - Reproduced on `main` (36e3ff3) with pymongo 4.19.0 against MongoDB 7.0: a 
script that builds a small app with `initialize_from` + `with_state_persister` 
and runs one step, once in a `with` block and once with `cleanup()` in a 
`finally`. Both raised the `AttributeError` above. With this change, both 
finish without errors.
   - The two new tests fail on `main` with that `AttributeError` and pass with 
this change. `test_b_mongodb.py` and `test_b_pymongo_driver_info.py` pass with 
`BURR_CI_INTEGRATION_TESTS=true` (14 passed). I ran these on macOS, Python 3.12.
   - pre-commit hooks (black, isort, flake8, ASF license headers) on the 
changed files.
   
   ## Notes
   
   - Before this change, the client stayed open until `__del__` closed it at 
interpreter shutdown, which printed `ImportError: sys.meta_path is None, Python 
is likely shutting down` from pymongo. That message is gone once `cleanup()` 
works.
   
   ## Checklist
   
   - [x] PR has an informative and human-readable title (this will be pulled 
into the release notes)
   - [x] Changes are limited to a single goal (no scope creep)
   - [x] Code passed the pre-commit check & code is left cleaner/nicer than 
when first encountered.
   - [x] Any _change_ in functionality is tested
   - [x] New functions are documented (with a description, list of inputs, and 
expected output)
   - [x] Placeholder code is flagged / future TODOs are captured in comments
   - [x] Project documentation has been updated if adding/changing 
functionality.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to