Skip to content

Commit 73e51ab

Browse files
nesitorhoh
authored andcommitted
Fix: Use context manager instead integrate it on try and catch.
1 parent 3bf413f commit 73e51ab

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

src/aleph/vm/pool.py

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -90,57 +90,54 @@ async def create_a_vm(
9090
) -> VmExecution:
9191
"""Create a new Aleph Firecracker VM from an Aleph function message."""
9292

93-
self.creation_lock.acquire()
93+
with self.creation_lock:
94+
# Check if an execution is already present for this VM, then return it.
95+
# Do not `await` in this section.
96+
current_execution = self.get_running_vm(vm_hash)
97+
if current_execution:
98+
return current_execution
99+
else:
100+
execution = VmExecution(
101+
vm_hash=vm_hash,
102+
message=message,
103+
original=original,
104+
snapshot_manager=self.snapshot_manager,
105+
systemd_manager=self.systemd_manager,
106+
persistent=persistent,
107+
)
108+
self.executions[vm_hash] = execution
94109

95-
# Check if an execution is already present for this VM, then return it.
96-
# Do not `await` in this section.
97-
current_execution = self.get_running_vm(vm_hash)
98-
if current_execution:
99-
return current_execution
100-
else:
101-
execution = VmExecution(
102-
vm_hash=vm_hash,
103-
message=message,
104-
original=original,
105-
snapshot_manager=self.snapshot_manager,
106-
systemd_manager=self.systemd_manager,
107-
persistent=persistent,
108-
)
109-
self.executions[vm_hash] = execution
110+
try:
111+
await execution.prepare()
112+
vm_id = self.get_unique_vm_id()
110113

111-
try:
112-
await execution.prepare()
113-
vm_id = self.get_unique_vm_id()
114+
if self.network:
115+
vm_type = VmType.from_message_content(message)
116+
tap_interface = await self.network.prepare_tap(vm_id, vm_hash, vm_type)
117+
await self.network.create_tap(vm_id, tap_interface)
118+
else:
119+
tap_interface = None
114120

115-
if self.network:
116-
vm_type = VmType.from_message_content(message)
117-
tap_interface = await self.network.prepare_tap(vm_id, vm_hash, vm_type)
118-
await self.network.create_tap(vm_id, tap_interface)
119-
else:
120-
tap_interface = None
121+
execution.create(vm_id=vm_id, tap_interface=tap_interface)
122+
await execution.start()
121123

122-
execution.create(vm_id=vm_id, tap_interface=tap_interface)
123-
await execution.start()
124+
# Start VM and snapshots automatically
125+
if execution.persistent:
126+
self.systemd_manager.enable_and_start(execution.controller_service)
127+
await execution.wait_for_init()
128+
if execution.is_program and execution.vm:
129+
await execution.vm.load_configuration()
124130

125-
# Start VM and snapshots automatically
126-
if execution.persistent:
127-
self.systemd_manager.enable_and_start(execution.controller_service)
128-
await execution.wait_for_init()
129-
if execution.is_program and execution.vm:
130-
await execution.vm.load_configuration()
131-
132-
if execution.vm and execution.vm.support_snapshot and self.snapshot_manager:
133-
await self.snapshot_manager.start_for(vm=execution.vm)
134-
except Exception:
135-
# ensure the VM is removed from the pool on creation error
136-
self.forget_vm(vm_hash)
137-
raise
138-
finally:
139-
self.creation_lock.release()
140-
141-
self._schedule_forget_on_stop(execution)
142-
143-
return execution
131+
if execution.vm and execution.vm.support_snapshot and self.snapshot_manager:
132+
await self.snapshot_manager.start_for(vm=execution.vm)
133+
except Exception:
134+
# ensure the VM is removed from the pool on creation error
135+
self.forget_vm(vm_hash)
136+
raise
137+
138+
self._schedule_forget_on_stop(execution)
139+
140+
return execution
144141

145142
def get_unique_vm_id(self) -> int:
146143
"""Get a unique identifier for the VM.

0 commit comments

Comments
 (0)