@@ -544,6 +544,32 @@ def test_decimal_not_returned_as_strings_arrow(self):
544544decimal_type = arrow_df .field (0 ).type
545545self .assertTrue (pyarrow .types .is_decimal (decimal_type ))
546546
547+ def test_close_connection_closes_cursors (self ):
548+
549+ from databricks .sql .thrift_api .TCLIService import ttypes
550+
551+ with self .connection ()as conn :
552+ cursor = conn .cursor ()
553+ cursor .execute ('SELECT id, id `id2`, id `id3` FROM RANGE(1000000) order by RANDOM()' )
554+ ars = cursor .active_result_set
555+
556+ # We must manually run this check because thrift_backend always forces `has_been_closed_server_side` to True
557+
558+ # Cursor op state should be open before connection is closed
559+ status_request = ttypes .TGetOperationStatusReq (operationHandle = ars .command_id ,getProgressUpdate = False )
560+ op_status_at_server = ars .thrift_backend ._client .GetOperationStatus (status_request )
561+ assert op_status_at_server .operationState != ttypes .TOperationState .CLOSED_STATE
562+
563+ conn .close ()
564+
565+ # When connection closes, any cursor operations should no longer exist at the server
566+ with self .assertRaises (thrift .Thrift .TApplicationException )as cm :
567+ op_status_at_server = ars .thrift_backend ._client .GetOperationStatus (status_request )
568+ if hasattr (cm ,"exception" ):
569+ assert "RESOURCE_DOES_NOT_EXIST" in cm .exception .message
570+
571+
572+
547573
548574# use a RetrySuite to encapsulate these tests which we'll typically want to run together; however keep
549575# the 429/503 subsuites separate since they execute under different circumstances.