FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix RecurrentThread no-interval loop referencing wrong attributes by winklemad · Pull Request #178 · unitreerobotics/unitree_sdk2_python · GitHub

Fix RecurrentThread no-interval loop referencing wrong attributes - #178

Open
winklemad wants to merge 1 commit into
unitreerobotics:masterfrom
winklemad:fix/recurrentthread-zero-interval-attrs
Open

winklemad wants to merge 1 commit into
unitreerobotics:masterfrom
winklemad:fix/recurrentthread-zero-interval-attrs

Conversation

Copy link
Copy Markdown

Problem

RecurrentThread.__LoopFunc_0 — the loop body used when interval is None or interval <= 0.0 — reads the wrong attributes:

def __LoopFunc_0(self):
    while not self.__quit:
        try:
            self.__loopTarget(*self.__args, **self.__kwargs)
        except:
            ...

The constructor stores the loop parameters as self.__loopArgs / self.__loopKwargs:

self.__loopArgs = args
self.__loopKwargs = {} if kwargs is None else kwargs

Because of Python name mangling, self.__args / self.__kwargs inside __LoopFunc_0 resolve to _RecurrentThread__args / _RecurrentThread__kwargs, which are never assigned on RecurrentThread (only the base Thread sets _Thread__args). So every iteration raises AttributeError, which is swallowed by the bare except. The result: the target never runs, and the thread spins in a hot loop printing the same error. The timer-based __LoopFunc (the interval > 0 path) correctly uses self.__loopArgs / self.__loopKwargs.

Reproduce

import time
from unitree_sdk2py.utils.thread import RecurrentThread

calls = []
rt = RecurrentThread(0, target=lambda: calls.append(1))   # interval <= 0 -> __LoopFunc_0
rt.Start()
time.sleep(0.3)
rt.Wait(timeout=1.0)
print(len(calls))
# 0, and stdout is filled with:
#   [RecurrentThread] target func raise exception: name=AttributeError,
#   args=("'RecurrentThread' object has no attribute '_RecurrentThread__args'",)

Fix

Use self.__loopArgs / self.__loopKwargs in __LoopFunc_0, matching __LoopFunc. After the fix the target runs each iteration and args / kwargs are forwarded correctly.

RecurrentThread.__LoopFunc_0 (used when interval is None or <= 0) called
self.__loopTarget(*self.__args, **self.__kwargs). Because of name mangling
those resolve to _RecurrentThread__args / _RecurrentThread__kwargs, which
are never set — the constructor stores the loop parameters as __loopArgs
and __loopKwargs. Every iteration raised AttributeError (swallowed by the
bare except), so the target never ran and the thread spun in a hot error
loop. Use __loopArgs / __loopKwargs, matching the timer-based __LoopFunc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant


Back | FazBrowse Home | New Git URL