DuckDB ResourceInUseException when using duckdb.sql() in Marimo

Open
#11,996 5 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
pandas, python
Domain
data

Research direction

Start with the DuckDB relation formatting path named in the report: marimo/_output/formatting.py and marimo/_output/formatters/repr_formatters.py. Reproduce with the provided DataFrame and duckdb.sql() example, then compare it with a query through duckdb.connect(). Done when automatic display succeeds for the sql() result without raising ResourceInUseException.

Written by the indexing model from the issue text.

Description

bug cannot reproduce
Describe the bug

I'm experiencing a ResourceInUseException when executing SQL queries using duckdb.sql() in Marimo.

The issue occurs when Marimo attempts to automatically format and display the resulting DuckDB Relation.

Interestingly, the same queries work as expected when using an explicit connection created with duckdb.connect().

Steps to Reproduce

Create a Pandas DataFrame.

Execute a SQL query using duckdb.sql().

Allow Marimo to automatically display the resulting relation.

Example:


import duckdb
import pandas as pd

df = pd.DataFrame({
    "id": [1, 2, 3],
    "value": [10, 20, 30]
})

result = duckdb.sql("""
    SELECT *
    FROM df
""")

result

Expected Behavior

The SQL query should execute successfully, and Marimo should display the resulting relation without raising an exception.

Actual Behavior

Marimo raises the following exception when attempting to format the DuckDB Relation:

_duckdb.ResourceInUseException: Resource In Use Error:
connection has an unfinished result from execute();
call complete() or abort(), or fetch until a fetch returns
no rows, before other work on this connection,
or use a separate connection from cursor()

The traceback points to Marimo's automatic formatting process:

marimo/_output/formatting.py
marimo/_output/formatters/repr_formatters.py
duckdb/_relation.py

Additional Findings
After testing different approaches, I noticed that:
The issue occurs when using duckdb.sql().
The same query works correctly when using duckdb.connect() and executing the query through the connection.
The exception appears during the automatic formatting/display of the DuckDB Relation in Marimo.

Environment
OS: Windows
Python: 3.15
Notebook environment: Marimo
Database engine: DuckDB

Question

Could this be related to how Marimo handles or formats DuckDB Relation objects returned by duckdb.sql(), particularly when the underlying connection has an unfinished result?

Is this an expected DuckDB limitation or a potential compatibility issue between DuckDB's relation handling and Marimo's reactive execution/display mechanism?

Will you submit a PR?
  • Yes
Environment
 {
  "marimo": "0.25.2",
  "editable": false,
  "location": "C:/Users/user/AppData/Local/Python/pythoncore-3.15-64/Lib/site-packages/marimo",
  "OS": "Windows",
  "OS Version": "11",
  "Processor": "Intel64 Family 6 Model 142 Stepping 12, GenuineIntel",
  "Python Version": "3.15.0",
  "Locale": "English_United States",
  "Binaries": {
    "Browser": "157.0.8103.79",
    "Node": "--",
    "uv": "0.13.7 (316c827e0 2026-11-03 x86_64-pc-windows-msvc)"
  },
  "Dependencies": {
    "click": "8.5.1",
    "docutils": "0.23",
    "itsdangerous": "2.2.0",
    "jedi": "0.20.1",
    "markdown": "3.11.3",
    "narwhals": "2.28.0",
    "packaging": "26.3",
    "psutil": "7.2.2",
    "pygments": "2.21.0",
    "pymdown-extensions": "11.0.2",
    "pyyaml": "6.0.3",
    "starlette": "1.10.0",
    "tomlkit": "0.15.2",
    "typing-extensions": "4.16.0",
    "uvicorn": "0.54.0",
    "websockets": "17.4"
  },
  "Optional Dependencies": {
    "altair": "6.3.0",
    "anywidget": "0.11.0",
    "duckdb": "2.0.0",
    "mcp": "2.3.0",
    "nbformat": "5.11.1",
    "openai": "3.51.0",
    "pandas": "3.1.1",
    "polars": "2.1.0",
    "pyarrow": "26.0.0",
    "pytest": "9.1.1",
    "python-lsp-ruff": "2.3.4",
    "python-lsp-server": "1.15.0",
    "ruff": "0.17.0",
    "sqlglot": "30.31.0",
    "vegafusion": "2.0.3"
  },
  "Experimental Flags": {}
}
Code to reproduce
import duckdb as db
db.sql('select * from df')
Dominant language
Python
Stars
22.9k
Forks
1.3k
Avg merge
1d 10h
Merged PRs (30d)
380

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from marimo-team/marimo

All issues in marimo-team/marimo

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.