public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH net] selftests: net: fix wrong boolean evaluation in __exit__
@ 2026-01-20 12:47 Gal Pressman
  2026-01-20 23:44 ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Gal Pressman @ 2026-01-20 12:47 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Andrew Lunn, netdev
  Cc: Simon Horman, Shuah Khan, Joe Damato, Stanislav Fomichev,
	linux-kselftest, Gal Pressman, Nimrod Oren

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.

Fixes: 6e9a12f85a75 ("selftests: net: terminate bkg() commands on exception")
Reviewed-by: Nimrod Oren <noren@nvidia.com>
Signed-off-by: Gal Pressman <gal@nvidia.com>
---
 tools/testing/selftests/net/lib/py/utils.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py
index 106ee1f2df86..dc1db78b5304 100644
--- a/tools/testing/selftests/net/lib/py/utils.py
+++ b/tools/testing/selftests/net/lib/py/utils.py
@@ -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)
 
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net] selftests: net: fix wrong boolean evaluation in __exit__
  2026-01-20 12:47 [PATCH net] selftests: net: fix wrong boolean evaluation in __exit__ Gal Pressman
@ 2026-01-20 23:44 ` Jakub Kicinski
  2026-01-21  7:11   ` Gal Pressman
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Kicinski @ 2026-01-20 23:44 UTC (permalink / raw)
  To: Gal Pressman
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Andrew Lunn, netdev,
	Simon Horman, Shuah Khan, Joe Damato, Stanislav Fomichev,
	linux-kselftest, Nimrod Oren

On Tue, 20 Jan 2026 14:47:33 +0200 Gal Pressman wrote:
> 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.

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.
-- 
pw-bot: cr

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net] selftests: net: fix wrong boolean evaluation in __exit__
  2026-01-20 23:44 ` Jakub Kicinski
@ 2026-01-21  7:11   ` Gal Pressman
  0 siblings, 0 replies; 3+ messages in thread
From: Gal Pressman @ 2026-01-21  7:11 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Andrew Lunn, netdev,
	Simon Horman, Shuah Khan, Joe Damato, Stanislav Fomichev,
	linux-kselftest, Nimrod Oren

On 21/01/2026 1:44, Jakub Kicinski wrote:
> On Tue, 20 Jan 2026 14:47:33 +0200 Gal Pressman wrote:
>> 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.
> 
> 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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-01-21  7:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-20 12:47 [PATCH net] selftests: net: fix wrong boolean evaluation in __exit__ Gal Pressman
2026-01-20 23:44 ` Jakub Kicinski
2026-01-21  7:11   ` Gal Pressman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox