Skip to content

Commit a668437

Browse files
committed
fix: remove container even if already removed
1 parent 407c1a1 commit a668437

1 file changed

Lines changed: 39 additions & 7 deletions

File tree

cubbi/container.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,21 @@ def _close_single_session(self, session: Session, kill: bool = False) -> bool:
915915
self.session_manager.remove_session(session.id)
916916
return True
917917
except DockerException as e:
918-
print(f"Error closing session {session.id}: {e}")
919-
return False
918+
error_message = str(e).lower()
919+
# If container is not running or already removed, still remove it from session list
920+
if (
921+
"is not running" in error_message
922+
or "no such container" in error_message
923+
or "not found" in error_message
924+
):
925+
print(
926+
f"Container already stopped/removed, removing session {session.id} from list"
927+
)
928+
self.session_manager.remove_session(session.id)
929+
return True
930+
else:
931+
print(f"Error closing session {session.id}: {e}")
932+
return False
920933

921934
def close_all_sessions(
922935
self, progress_callback=None, kill: bool = False
@@ -980,11 +993,30 @@ def close_with_progress(session):
980993

981994
return True
982995
except DockerException as e:
983-
error_msg = f"Error: {str(e)}"
984-
if progress_callback:
985-
progress_callback(session.id, "failed", error_msg)
986-
print(f"Error closing session {session.id}: {e}")
987-
return False
996+
error_message = str(e).lower()
997+
# If container is not running or already removed, still remove it from session list
998+
if (
999+
"is not running" in error_message
1000+
or "no such container" in error_message
1001+
or "not found" in error_message
1002+
):
1003+
print(
1004+
f"Container already stopped/removed, removing session {session.id} from list"
1005+
)
1006+
self.session_manager.remove_session(session.id)
1007+
if progress_callback:
1008+
progress_callback(
1009+
session.id,
1010+
"completed",
1011+
f"{session.name} removed from list (container already stopped)",
1012+
)
1013+
return True
1014+
else:
1015+
error_msg = f"Error: {str(e)}"
1016+
if progress_callback:
1017+
progress_callback(session.id, "failed", error_msg)
1018+
print(f"Error closing session {session.id}: {e}")
1019+
return False
9881020

9891021
# Use ThreadPoolExecutor to close sessions in parallel
9901022
with concurrent.futures.ThreadPoolExecutor(

0 commit comments

Comments
 (0)