- 
                Notifications
    You must be signed in to change notification settings 
- Fork 749
fix: duckdb interrupt #6806
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
          
     Merged
      
      
    
  
     Merged
                    fix: duckdb interrupt #6806
Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b181898
              
                fix: duckdb interruptions
              
              
                akshayka 2c0887f
              
                Unit tests
              
              
                akshayka 47c9a60
              
                Install duckdb connection in duckdb engine (post execution hooks)
              
              
                akshayka 56eee0c
              
                add `__init__.py__` to test folder for pytest
              
              
                akshayka 6beba61
              
                maybe fix CI
              
              
                akshayka File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import marimo | ||
|  | ||
| __generated_with = "0.16.5" | ||
| app = marimo.App(width="medium") | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(): | ||
| FILE = "https://data.source.coop/cholmes/eurocrops/unprojected/geoparquet/FR_2018_EC21.parquet" | ||
| return (FILE,) | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(): | ||
| import marimo as mo | ||
| return (mo,) | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(mo): | ||
| _df = mo.sql( | ||
| f""" | ||
| INSTALL spatial; | ||
| LOAD spatial; | ||
| """ | ||
| ) | ||
| return | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(): | ||
| 10 | ||
| return | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(mo): | ||
| _df = mo.sql( | ||
| f""" | ||
| SELECT 1; | ||
| """ | ||
| ) | ||
| return | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(FILE, mo, null): | ||
| _df = mo.sql( | ||
| f""" | ||
| CREATE TABLE gdf AS | ||
| SELECT * | ||
| FROM '{FILE}' | ||
| """ | ||
| ) | ||
| return | ||
|  | ||
|  | ||
| if __name__ == "__main__": | ||
| app.run() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              Empty file.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Copyright 2025 Marimo. All rights reserved. | ||
| from __future__ import annotations | ||
|  | ||
| import signal | ||
| from unittest.mock import MagicMock, patch | ||
|  | ||
| import pytest | ||
|  | ||
| from marimo._dependencies.dependencies import DependencyManager | ||
| from marimo._runtime.context.types import ExecutionContext | ||
| from marimo._runtime.handlers import construct_interrupt_handler | ||
| from marimo._runtime.runtime import MarimoInterrupt | ||
|  | ||
| HAS_DUCKDB = DependencyManager.duckdb.has() | ||
|  | ||
|  | ||
| @pytest.mark.skipif(not HAS_DUCKDB, reason="DuckDB not installed") | ||
| def test_duckdb_interrupt_handler_called_when_connection_present(): | ||
| """Test that duckdb.interrupt() is called when a connection is present.""" | ||
| import duckdb | ||
|  | ||
| # Create a mock connection that we can spy on | ||
| mock_conn = MagicMock(spec=duckdb.DuckDBPyConnection) | ||
|  | ||
| # Create an execution context with a duckdb connection | ||
| exec_ctx = ExecutionContext(cell_id="cell_id", setting_element_value=False) | ||
|  | ||
| # Mock the context to return our execution context | ||
| with patch("marimo._runtime.handlers.get_context") as mock_get_context: | ||
| mock_context = MagicMock() | ||
| mock_context.execution_context = exec_ctx | ||
| mock_get_context.return_value = mock_context | ||
|  | ||
| # Verify interrupt() is called when connection is set | ||
| with exec_ctx.with_connection(mock_conn): | ||
| interrupt_handler = construct_interrupt_handler(mock_context) | ||
|  | ||
| # Trigger the interrupt handler | ||
| with pytest.raises(MarimoInterrupt): | ||
| interrupt_handler(signal.SIGINT, None) | ||
|  | ||
| # Verify duckdb connection's interrupt was called | ||
| mock_conn.interrupt.assert_called_once() | ||
|  | ||
|  | ||
| @pytest.mark.skipif(not HAS_DUCKDB, reason="DuckDB not installed") | ||
| def test_duckdb_interrupt_handler_no_error_when_connection_none(): | ||
| """Test that no error occurs when connection is None.""" | ||
| # Create an execution context without a connection | ||
| exec_ctx = ExecutionContext(cell_id="cell_id", setting_element_value=False) | ||
| exec_ctx.duckdb_connection = None | ||
|  | ||
| # Mock the context to return our execution context | ||
| with patch("marimo._runtime.handlers.get_context") as mock_get_context: | ||
| mock_context = MagicMock() | ||
| mock_context.execution_context = exec_ctx | ||
| mock_get_context.return_value = mock_context | ||
|  | ||
| interrupt_handler = construct_interrupt_handler(mock_context) | ||
|  | ||
| # Should not raise error from duckdb interrupt (only MarimoInterrupt) | ||
| with pytest.raises(MarimoInterrupt): | ||
| interrupt_handler(signal.SIGINT, None) | ||
|  | ||
|  | ||
| @pytest.mark.skipif(not HAS_DUCKDB, reason="DuckDB not installed") | ||
| def test_duckdb_interrupt_handler_exception_handling(): | ||
| """Test that exceptions during interrupt() don't crash the kernel.""" | ||
| import duckdb | ||
|  | ||
| # Create a mock connection that raises an exception | ||
| mock_conn = MagicMock(spec=duckdb.DuckDBPyConnection) | ||
| mock_conn.interrupt.side_effect = RuntimeError("Mock error") | ||
|  | ||
| # Create an execution context with a duckdb connection | ||
| exec_ctx = ExecutionContext(cell_id="cell_id", setting_element_value=False) | ||
|  | ||
| # Mock the context to return our execution context | ||
| with patch("marimo._runtime.handlers.get_context") as mock_get_context: | ||
| mock_context = MagicMock() | ||
| mock_context.execution_context = exec_ctx | ||
| mock_get_context.return_value = mock_context | ||
|  | ||
| # Make interrupt() raise an exception | ||
| with exec_ctx.with_connection(mock_conn): | ||
| interrupt_handler = construct_interrupt_handler(mock_context) | ||
|  | ||
| # Should raise MarimoInterrupt, not RuntimeError | ||
| # The RuntimeError should be caught and logged | ||
| with pytest.raises(MarimoInterrupt): | ||
| interrupt_handler(signal.SIGINT, None) | ||
|  | ||
| # Verify interrupt was attempted | ||
| mock_conn.interrupt.assert_called_once() | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.