selftests: net: fix wrong boolean evaluation in __exit__
Subsystems (3)
PATCH 1/1 selftests: net: fix wrong boolean evaluation in __exit__
The __exit__ method receives ex_type as the exception class when an exception occurs. The previous code used implicit boolean evaluation:
terminate = self.terminate or (self._exit_wait and ex_type)
^^^^^^^^^^^
In Python, the and operator can be used with non-boolean values, but it does not always return a boolean result.
This is probably not what we want, because 'self._exit_wait and ex_type' could return the actual ex_type value (the exception class) rather than a boolean True when an exception occurs.
Use explicit ex_type is not None check to properly evaluate whether an exception occurred, returning a boolean result.
--- tools/testing/selftests/net/lib/py/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/net/lib/py/utils.py +1 -1
@@ -137,7 +137,7 @@ class bkg(cmd): def __exit__(self, ex_type, ex_value, ex_tb): # Force termination on exception - terminate = self.terminate or (self._exit_wait and ex_type) + terminate = self.terminate or (self._exit_wait and ex_type is not None) return self.process(terminate=terminate, fail=self.check_fail)
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerQuoted 9 lines · On Tue, 20 Jan 2026 14:47:33 +0200 Gal Pressman wrote:
The
__exit__method receivesex_typeas the exception class when an exception occurs. The previous code used implicit boolean evaluation:terminate = self.terminate or (self._exit_wait and
ex_type)
^^^^^^^^^^^In Python, the and operator can be used with non-boolean values, but it does not always return a boolean result.
Quoted 6 lines
This is probably not what we want, because 'self._exit_wait and
ex_type' could return the actualex_typevalue (the exception class) rather than a boolean True when an exception occurs.Use explicit
ex_type is not Nonecheck to properly evaluate whether an exception occurred, returning a boolean result.
Sure, the checkers complain about this, but I don't see an actual bug here. bool(terminate) must evaluate correctly, we don't compare it to True or False explicitly.
To be clear - the patch LGTM, I'm just not connecting the dots on why its a fix at this stage.
Signature · 1 line
pw-bot: cr
Quoted 17 lines · Jakub Kicinski
Quoted 9 lines · On Tue, 20 Jan 2026 14:47:33 +0200 Gal Pressman wrote:
The
__exit__method receivesex_typeas the exception class when an exception occurs. The previous code used implicit boolean evaluation:terminate = self.terminate or (self._exit_wait and
ex_type)
^^^^^^^^^^^In Python, the and operator can be used with non-boolean values, but it does not always return a boolean result.
Quoted 6 lines
This is probably not what we want, because 'self._exit_wait and
ex_type' could return the actualex_typevalue (the exception class) rather than a boolean True when an exception occurs.Use explicit
ex_type is not Nonecheck to properly evaluate whether an exception occurred, returning a boolean result.
Quoted 6 lines
Sure, the checkers complain about this, but I don't see an actual bug here. bool(terminate) must evaluate correctly, we don't compare it to True or False explicitly.
To be clear - the patch LGTM, I'm just not connecting the dots on why its a fix at this stage.
Right, the code probably works regardless of this fix. The reason I submitted this as a fix is because surely there was no intention for 'terminate' to be non-boolean.
Take the patch to net-next instead?
BTW, I encountered this issue while debugging some tests and dumping various states, the value of 'terminate' confused me.