Skip to content

Conversation

cheukt
Copy link
Member

@cheukt cheukt commented Aug 22, 2023

instead of within the same thread (using the event loop), since that can be crowded out if thread is blocked

only one shared var, and we only need it to track 3 states: True, False, None. Made an enum to track

@cheukt cheukt requested a review from a team as a code owner August 22, 2023 21:33
@cheukt cheukt requested review from maximpertsov, stuqdog and njooma and removed request for stuqdog August 22, 2023 21:33
Copy link
Contributor

@maximpertsov maximpertsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! just some minor suggestions

Copy link
Contributor

@maximpertsov maximpertsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my test suggestion is [opt] if it ends up being too cumbersome

Copy link
Contributor

@maximpertsov maximpertsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Copy link
Member

@njooma njooma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good! I know @dgottlieb had thoughts on process vs thread

@cheukt
Copy link
Member Author

cheukt commented Aug 30, 2023

after talking to @dgottlieb a bit more, will try to make this a thread

@cheukt cheukt requested review from njooma and maximpertsov August 30, 2023 19:49
@cheukt
Copy link
Member Author

cheukt commented Aug 30, 2023

rerequested review since I changed to using threads, which simplifies the code quite a bit

@cheukt cheukt changed the title RSDK-4455 - Run session heartbeat loop in a process RSDK-4455 - Run session heartbeat loop in a thread Aug 30, 2023
@cheukt cheukt changed the title RSDK-4455 - Run session heartbeat loop in a thread RSDK-4455 - Run session heartbeat loop in separate thread Aug 30, 2023
Comment on lines +126 to +132
self._thread = Thread(
name="heartbeat-thread",
target=asyncio.run,
args=(self._heartbeat_process(wait),),
daemon=True,
)
self._thread.start()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: is it possible to get here with an active thread? (e.g. self._thread is not None). If so, do we need to do any cleanup?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is possible, added a reset if so

Comment on lines 56 to 61
self._address = address
self._dial_options = dial_options

self._lock: Lock = Lock()
self._supported: _SupportedState = _SupportedState.UNKNOWN
self._thread: Optional[Thread] = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to define these above with the other instance vars

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved, also moved some of the class vars into this function since I don't think they should've been class vars to begin with

name="heartbeat-thread",
target=asyncio.run,
args=(self._heartbeat_process(wait),),
daemon=True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this should be daemonized?

Copy link
Member Author

@cheukt cheukt Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main issue I ran into a non-daemonized thread is that it doesn't stop on a ctrl+c, and there doesn't seem to be a place to catch to interrupt and kill the thread either? since the daemonic thread just dies on program exit, the cleanup can be handled like normal (since the one rust connection is reused).


channel = await dial(address=self._address, options=dial_options)
client = RobotServiceStub(channel.channel)
while self._supported == _SupportedState.TRUE:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this check need to be in a lock?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a lock

cheukt and others added 4 commits August 30, 2023 18:53
@cheukt cheukt requested review from maximpertsov and njooma August 31, 2023 17:35
@cheukt cheukt merged commit 6ceb8d7 into viamrobotics:main Sep 11, 2023
@cheukt cheukt deleted the heartbeat-fix branch September 11, 2023 17:34
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.

3 participants