diff mbox series

[v2,2/6] python/machine: close sock_pair in cleanup path

Message ID 20230725180337.2937292-3-jsnow@redhat.com
State New
Headers show
Series python/machine: use socketpair() for console socket | expand

Commit Message

John Snow July 25, 2023, 6:03 p.m. UTC
If everything has gone smoothly, we'll already have closed the socket we
gave to the child during post_launch. The other half of the pair that we
gave to the QMP connection should, likewise, be definitively closed by
now.

However, in the cleanup path, it's possible we've created the socketpair
but flubbed the launch and need to clean up resources. These resources
*would* be handled by the garbage collector, but that can happen at
unpredictable times. Nicer to just clean them up synchronously on the
exit path, here.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 python/qemu/machine/machine.py | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Daniel P. Berrangé July 25, 2023, 6:18 p.m. UTC | #1
On Tue, Jul 25, 2023 at 02:03:33PM -0400, John Snow wrote:
> If everything has gone smoothly, we'll already have closed the socket we
> gave to the child during post_launch. The other half of the pair that we
> gave to the QMP connection should, likewise, be definitively closed by
> now.
> 
> However, in the cleanup path, it's possible we've created the socketpair
> but flubbed the launch and need to clean up resources. These resources
> *would* be handled by the garbage collector, but that can happen at
> unpredictable times. Nicer to just clean them up synchronously on the
> exit path, here.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  python/qemu/machine/machine.py | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/python/qemu/machine/machine.py b/python/qemu/machine/machine.py
> index 8be0f684fe..26f0fb8a81 100644
> --- a/python/qemu/machine/machine.py
> +++ b/python/qemu/machine/machine.py
> @@ -395,6 +395,11 @@ def _post_shutdown(self) -> None:
>          finally:
>              assert self._qmp_connection is None
>  
> +        if self._sock_pair:
> +            self._sock_pair[0].close()
> +            self._sock_pair[1].close()
> +            self._sock_pair = None
> +

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

With regards,
Daniel
Ani Sinha July 26, 2023, 7:23 a.m. UTC | #2
> On 25-Jul-2023, at 11:48 PM, Daniel P. Berrangé <berrange@redhat.com> wrote:
> 
> On Tue, Jul 25, 2023 at 02:03:33PM -0400, John Snow wrote:
>> If everything has gone smoothly, we'll already have closed the socket we
>> gave to the child during post_launch. The other half of the pair that we
>> gave to the QMP connection should, likewise, be definitively closed by
>> now.
>> 
>> However, in the cleanup path, it's possible we've created the socketpair
>> but flubbed the launch and need to clean up resources. These resources
>> *would* be handled by the garbage collector, but that can happen at
>> unpredictable times. Nicer to just clean them up synchronously on the
>> exit path, here.
>> 
>> Signed-off-by: John Snow <jsnow@redhat.com>

Reviewed-by: Ani Sinha <anisinha@redhat.com>

>> ---
>> python/qemu/machine/machine.py | 5 +++++
>> 1 file changed, 5 insertions(+)
>> 
>> diff --git a/python/qemu/machine/machine.py b/python/qemu/machine/machine.py
>> index 8be0f684fe..26f0fb8a81 100644
>> --- a/python/qemu/machine/machine.py
>> +++ b/python/qemu/machine/machine.py
>> @@ -395,6 +395,11 @@ def _post_shutdown(self) -> None:
>>         finally:
>>             assert self._qmp_connection is None
>> 
>> +        if self._sock_pair:
>> +            self._sock_pair[0].close()
>> +            self._sock_pair[1].close()
>> +            self._sock_pair = None
>> +
> 
> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
> 
> With regards,
> Daniel
> -- 
> |: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
> |: https://libvirt.org         -o-            https://fstop138.berrange.com :|
> |: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
>
diff mbox series

Patch

diff --git a/python/qemu/machine/machine.py b/python/qemu/machine/machine.py
index 8be0f684fe..26f0fb8a81 100644
--- a/python/qemu/machine/machine.py
+++ b/python/qemu/machine/machine.py
@@ -395,6 +395,11 @@  def _post_shutdown(self) -> None:
         finally:
             assert self._qmp_connection is None
 
+        if self._sock_pair:
+            self._sock_pair[0].close()
+            self._sock_pair[1].close()
+            self._sock_pair = None
+
         self._close_qemu_log_file()
 
         self._load_io_log()