Skip to content

Add close_db_connections method#1261

Merged
giswqs merged 4 commits into
masterfrom
db
Nov 14, 2025
Merged

Add close_db_connections method#1261
giswqs merged 4 commits into
masterfrom
db

Conversation

@giswqs

@giswqs giswqs commented Nov 14, 2025

Copy link
Copy Markdown
Member

No description provided.

Copilot AI review requested due to automatic review settings November 14, 2025 14:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds functionality to close DuckDB database connections, addressing file lock issues when multiple programs need to access the same database. The implementation introduces a global registry to track connection pools and provides methods to explicitly close connections.

  • Adds close_duckdb_connections() function in common.py to close connection pools for DuckDB databases
  • Implements close_db_connections() method in maplibregl.Map to close connections for databases used by a specific map instance
  • Tracks database paths in _duckdb_databases list to enable per-map connection management

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 7 comments.

File Description
leafmap/maplibregl.py Adds database path tracking and close_db_connections() method to Map class for managing DuckDB connection lifecycle
leafmap/common.py Implements global connection pool registry and close_duckdb_connections() function for closing database connections
docs/maplibre/duckdb_layer.ipynb Adds example calls to close_db_connections() after using DuckDB layers; updates Python version metadata
.gitignore Adds *.zip and *.db file patterns to ignore database and archive files

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread leafmap/maplibregl.py

traceback.print_exc()

def close_db_connections(self, database_path: str = None, quiet: bool = False):

Copilot AI Nov 14, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] The quiet parameter defaults to False in close_db_connections, but it defaults to True in the underlying close_duckdb_connections function in common.py. This inconsistency could be confusing. Consider aligning the default values for consistency, or document why they differ.

Copilot uses AI. Check for mistakes.
Comment thread leafmap/common.py Outdated
Comment thread leafmap/common.py Outdated
Comment on lines +13197 to +13207
# Close all connections in the pool
closed_count = 0
while not connection_pool.empty():
try:
con = connection_pool.get_nowait()
con.close()
closed_count += 1
except Exception as e:
if not quiet:
print(f"Error closing connection: {e}")

Copilot AI Nov 14, 2025

Copy link

Choose a reason for hiding this comment

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

The connection closing logic only closes connections that are currently in the pool (idle connections). If connections are currently being used by active requests (checked out from the pool), they won't be closed. Consider adding logic to wait for active connections to be returned or documenting this limitation in the function docstring.

Copilot uses AI. Check for mistakes.
Comment thread leafmap/common.py Outdated
Comment on lines +13208 to +13209
# Remove from registry
del _duckdb_connection_pools[db_path]

Copilot AI Nov 14, 2025

Copy link

Choose a reason for hiding this comment

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

After removing the database from the registry, the tile server Flask app that was started in start_duckdb_tile_server will continue running in its daemon thread but will no longer have access to connections. This could lead to errors when the server tries to serve tiles. Consider documenting that the tile server needs to be manually stopped or restarted after closing connections.

Copilot uses AI. Check for mistakes.
Comment thread leafmap/maplibregl.py
Comment on lines +3337 to +3338
if db_path not in self._duckdb_databases:
self._duckdb_databases.append(db_path)

Copilot AI Nov 14, 2025

Copy link

Choose a reason for hiding this comment

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

The database path tracking uses a list with a membership check, which has O(n) time complexity. For better performance with multiple databases, consider using a set instead:

self._duckdb_databases = set()  # in __init__
# Then use:
self._duckdb_databases.add(db_path)

This would also automatically prevent duplicates without the explicit check.

Copilot uses AI. Check for mistakes.
Comment thread leafmap/maplibregl.py Outdated
Comment thread leafmap/common.py Outdated
@github-actions

github-actions Bot commented Nov 14, 2025

Copy link
Copy Markdown

@github-actions github-actions Bot temporarily deployed to pull request November 14, 2025 14:48 Inactive
giswqs and others added 3 commits November 14, 2025 09:49
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions github-actions Bot temporarily deployed to pull request November 14, 2025 14:55 Inactive
@giswqs giswqs merged commit 0cbad90 into master Nov 14, 2025
16 checks passed
@giswqs giswqs deleted the db branch November 14, 2025 14:55
giswqs added a commit that referenced this pull request Dec 6, 2025
* Add close_db_connections method

* Update leafmap/common.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update leafmap/common.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update leafmap/maplibregl.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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