Skip to contentSkip to conversation

Fix StatusChangeCallback so it works without a LogCallback

PATCH David Sommerseth 2 participants 5 messages Sep 4, 2023 – Sep 5, 2023 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#1 lore raw

[re-sent to ML]

Quoted 28 lines · Jeremy Fleischman
Quoted 17 lines
src/python/openvpn3/SessionManager.py +1 -0
index 3632790..1a567be 100644
@@ -114,6 +114,7 @@ def __init__(self, dbuscon, objpath):
          self.__log_callback = None
          self.__status_callback = None
          self.__deleted = False
+        self.__log_forward_enabled = False
 
 
      def __del__(self):
@@ -291,16 +292,11 @@ def LogCallback(self, cbfnc):
 
 dbus_interface='net.openvpn.v3.backends',
                                                 bus_name='net.openvpn.v3.log',

src/python/openvpn3/SessionManager.py def LogCallback(self, cbfnc): · line 298

Quoted 10 lines
src/python/openvpn3/SessionManager.py +0 -6
                                                 path=self.__session_path)
-            self.__session_intf.LogForward(True)
          else:
-            try:
-                self.__session_intf.LogForward(False)
-            except dbus.exceptions.DBusException:
-                # If this fails, the session is typically already removed
-                pass
              self.__dbuscon.remove_signal_receiver(self.__log_callback, 'Log')
              self.__log_callback = None
Quoted 7 lines

Oops, this code unconditionally removes the callback, even if there isn't currently a callback. The code below in StatusChangeCallback first checks if there is currently a callback registered before removing it. If self.__dbuscon.remove_signal_receiver is resilient to getting passed None values for callbacks, then I suppose would could skip the check and just unconditionally remove the callback.
Let me know what's best.

If you look at my proposal; in the LogCallback() method, I have this check:

if cbfnc is not None and self.__log_callback is None:

while you only do:

if cbfnc is not None:

I believe you need to check both (more below on that). And there is a similar logic check when disabling the callback:

elif cbfnc is None and self.__log_callback is not None:

I believe this is the trap you fell into. Your new patch just do an "else:"

The additional final check of setting a new callback without disabling the previous one can avoid some additional trouble. IIRC, the Python D-Bus API will actually call multiple callbacks if add_signal_receiver() is called more times with different callback methods. To support this, the LogCallback() and StatusChangeCallback() methods would need to track each callback function independently. I did not consider this a needed feature just yet; hence the additional final check:

elif (cbfnc is not None and self.__log_callback is not None):
# In this case, the program must first disable the # current LogCallback() before setting a new one. raise RuntimeError('LogCallback() is already enabled')

This should enforce only one callback function being enabled this way, and if the callback need to be changed - it's enough to first do disable it (LogCallback(None)) before setting the new one. And if more callbacks functions is wanted/needed, the additional ones can be called via the callback function registered with the LogCallback(). No need to make this code more complicated.

Otherwise, I like what you did to __set_log_forward(). That makes sense!

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#2 lore raw
Quoted 11 lines

elif (cbfnc is not None and self.__log_callback is not None):
# In this case, the program must first disable the # current LogCallback() before setting a new one. raise RuntimeError('LogCallback() is already enabled')

This should enforce only one callback function being enabled this way, and if the callback need to be changed - it's enough to first do disable it (LogCallback(None)) before setting the new one. And if more callbacks functions is wanted/needed, the additional ones can be called via the callback function registered with the LogCallback(). No need to make this code more complicated.

That makes sense! I *think* things get a little cleaner if we move this check to the top of the relevant functions (StatusChangeCallback and LogCallback). Basically, by first eliminating the possibility that we're changing an existing registered dbus callback, then we don't actually need to be as careful later on. For example, here's an updated version of LogCallback:

def LogCallback(self, cbfnc):
if cbfnc is not None and self.__log_callback is not None:
# In this case, the program must first disable the # current LogCallback() before setting a new one. raise RuntimeError('LogCallback() is already enabled')

if cbfnc is not None:
self.__log_callback = cbfnc self.__dbuscon.add_signal_receiver(cbfnc,
signal_name='Log',

dbus_interface='net.openvpn.v3.backends',
bus_name='net.openvpn.v3.log', path=self.__session_path)
else:
# Only remove the callback if there actually *is* a callback # currently.
if self.__log_callback is not None:

self.__dbuscon.remove_signal_receiver(self.__log_callback, 'Log')
self.__log_callback = None

self.__set_log_forward()

Does this seem safe?

I'm not familiar with path email etiquette/best practices. Let me know if/when I should send a fully updated patch.

#3 lore raw
Quoted 38 lines · Jeremy Fleischman
Quoted 11 lines

elif (cbfnc is not None and self.__log_callback is not None):
# In this case, the program must first disable the # current LogCallback() before setting a new one. raise RuntimeError('LogCallback() is already enabled')

This should enforce only one callback function being enabled this way, and if the callback need to be changed - it's enough to first do disable it (LogCallback(None)) before setting the new one. And if more callbacks functions is wanted/needed, the additional ones can be called via the callback function registered with the LogCallback(). No need to make this code more complicated.

That makes sense! I *think* things get a little cleaner if we move this check to the top of the relevant functions (StatusChangeCallback and LogCallback). Basically, by first eliminating the possibility that we're changing an existing registered dbus callback, then we don't actually need to be as careful later on. For example, here's an updated version of LogCallback:

def LogCallback(self, cbfnc):
if cbfnc is not None and self.__log_callback is not None:
# In this case, the program must first disable the # current LogCallback() before setting a new one. raise RuntimeError('LogCallback() is already enabled')

if cbfnc is not None:
self.__log_callback = cbfnc self.__dbuscon.add_signal_receiver(cbfnc,
signal_name='Log', dbus_interface='net.openvpn.v3.backends', bus_name='net.openvpn.v3.log', path=self.__session_path)
else:
# Only remove the callback if there actually *is* a callback
# currently.
if self.__log_callback is not None:
self.__dbuscon.remove_signal_receiver(self.__log_callback, 'Log') self.__log_callback = None

Quoted 3 lines

self.__set_log_forward()

Does this seem safe?

I have a feeling we're missing a check here.

The first check (with the raise RuntimeError), cbfnc and __log_callback are both set.

Next check will enable the signal receiver if cbfnc is set. The previous check enforces __log_callback to not be set in this case; this is probably fine - but should have a comment of this condition.

The else: clause I'm less convinced about. If cbfnc is None, __log_callback can also be None, which would trigger a remove_signal_receiver() call. We should avoid that.

Quoted 2 lines

I'm not familiar with path email etiquette/best practices. Let me know if/when I should send a fully updated patch.

So far, we've discussed possible solutions - so it has been fine doing it like this now. But I think with this last round, we can go for a v2 patch.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc

A message that is not in the archive (sent off-list or not yet received): 32f74cd6.AVMAACPKxlQAAAAAAAAAAW9e-ykAAYCsnhYAAAAAABXTlQBk9mOS@...2677...

#5 lore raw
Quoted 1 line

I have a feeling we're missing a check here.

Got it! You're right, I've added that in the v2 version of this patch (see below).

(quoted diff) +0 -0

b/src/python/openvpn3/SessionManager.py
index 3632790..a175015 100644

src/python/openvpn3/SessionManager.py +1 -0
@@ -114,6 +114,7 @@ def __init__(self, dbuscon, objpath):
         self.__log_callback = None
         self.__status_callback = None
         self.__deleted = False
+        self.__log_forward_enabled = False
 
 
     def __del__(self):
@@ -284,6 +285,16 @@ def GetFormattedStatistics(self,

prefix='Connection statistics:\n', format_str='

     #
     #
     def LogCallback(self, cbfnc):
+        if cbfnc is not None and self.__log_callback is not None:
+            # In this case, the program must first disable the
+            # current LogCallback() before setting a new one.
+            raise RuntimeError('LogCallback() is already enabled')
+
+        if cbfnc is None and self.__log_callback is None:
+            # This is fine: disabling a callback when there is no callback is a
+            # simple no-op.
+            return

+
if cbfnc is not None:
self.__log_callback = cbfnc self.__dbuscon.add_signal_receiver(cbfnc,
@@ -291,16 +302,14 @@ def LogCallback(self, cbfnc):

dbus_interface='net.openvpn.v3.backends',

                                                bus_name='net.openvpn.v3.log',
                                                path=self.__session_path)

- self.__session_intf.LogForward(True)
else:
- try:
- self.__session_intf.LogForward(False)
- except dbus.exceptions.DBusException:
- # If this fails, the session is typically already removed
- pass

-            self.__dbuscon.remove_signal_receiver(self.__log_callback, 'Log')
-            self.__log_callback = None
+            # Only remove the callback if there actually *is* a callback
+            # currently.
+            if self.__log_callback is not None:

+ self.__dbuscon.remove_signal_receiver(self.__log_callback, 'Log')
+ self.__log_callback = None

+ self.__set_log_forward()

##
# Subscribes to the StatusChange signals for this session and register
@@ -310,6 +319,16 @@ def LogCallback(self, cbfnc):
# (integer) StatusMajor, (integer) StatusMinor, (string) message
#
def StatusChangeCallback(self, cbfnc):
+ if cbfnc is not None and self.__status_callback is not None:

+            # In this case, the program must first disable the
+            # current StatusChangeCallback() before setting a new one.
+            raise RuntimeError('StatusChangeCallback() is already enabled')
+
+        if cbfnc is None and self.__status_callback is None:
+            # This is fine: disabling a callback when there is no callback is a
+            # simple no-op.
+            return

+
if cbfnc is not None:
self.__status_callback = cbfnc self.__dbuscon.add_signal_receiver(cbfnc,
@@ -318,10 +337,14 @@ def StatusChangeCallback(self, cbfnc):

                                                bus_name='net.openvpn.v3.log',
                                                path=self.__session_path)
         else:
-            self.__dbuscon.remove_signal_receiver(self.__status_callback,
-                                                  'StatusChange')
-            self.__status_callback = None
+            # Only remove the callback if there actually *is* a callback
+            # currently.
+            if self.__status_callback is not None:

+ self.__dbuscon.remove_signal_receiver(self.__status_callback,
+ 'StatusChange')
+ self.__status_callback = None

+ self.__set_log_forward()

     ##

@@ -417,6 +440,30 @@ def GetDCO(self):
def SetDCO(self, dco):
self.__prop_intf.Set('net.openvpn.v3.sessions', 'dco', dco)

+    ##
+    #  Internal method to enable/disable LogForward as needed.
+    #  Must be called whenever a callback that needs LogForward enabled is
+    #  added or removed.
+    #
+    def __set_log_forward(self):
+        # The LogCallback and the StatusChangeCallback both need LogForward
+        # enabled. In other words, LogForward should be enabled iff one or both
+        # of those callbacks are registered.
+        should_log_forward_be_enabled = (

+ self.__log_callback is not None or self.__status_callback
is not None
+ )
+
+ if should_log_forward_be_enabled and not self.__log_forward_enabled:
+ self.__session_intf.LogForward(True)
+ self.__log_forward_enabled = True
+ elif not should_log_forward_be_enabled and self.__log_forward_enabled:
+ try:
+ self.__session_intf.LogForward(False)
+ except dbus.exceptions.DBusException:
+ # If this fails, the session is typically already removed
+ pass
+
+ self.__log_forward_enabled = False

##

#6 lore raw
Quoted 4 lines · Jeremy Fleischman
Quoted 1 line

I have a feeling we're missing a check here.

Got it! You're right, I've added that in the v2 version of this patch (see below).

Could you send this via git-send-email, I think this is getting ready to be pulled in now.

   $ git send-email -v2 \
     --in-reply-to [email protected]...

Thx!

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc