Skip to content

Convert Node and Context to use C++ Classes - #771

Merged
ahcorde merged 12 commits into
post_galactic_freezefrom
ahcorde/pybind11-node
Apr 28, 2021
Merged

ahcorde merged 12 commits into
post_galactic_freezefrom
ahcorde/pybind11-node

Conversation

@ahcorde

@ahcorde ahcorde commented Apr 15, 2021 •

Copy link
Copy Markdown
Contributor

Related to #665

This converts the Node and Context functions to a C++ class.

This PR depends on this other PR #728 and include the changes of this branch about GuardCondition to use C++ classes.

There is one test which is failing:

  • test_declare_qos_parameters_with_unhappy_callback

I will create this as a draft, we need to decide if we are going to backport the signal PR or find a workaround.

@ahcorde ahcorde added the enhancement New feature or request label Apr 15, 2021
@ahcorde
ahcorde requested review from hidmic and sloretz April 15, 2021 16:36
@ahcorde ahcorde self-assigned this Apr 15, 2021
@ahcorde ahcorde mentioned this pull request Apr 15, 2021
32 of 34 tasks
@ahcorde
ahcorde force-pushed the ahcorde/pybind11-node branch from e9365a8 to 527fb10 Compare April 16, 2021 16:49
@ahcorde
ahcorde marked this pull request as ready for review April 16, 2021 16:50
@ahcorde

ahcorde commented Apr 16, 2021

Copy link
Copy Markdown
Contributor Author

The GuardCondition PR is in, I open this PR for review.

The test test_declare_qos_parameters_with_unhappy_callback is still failing:, do you mind to have a look @hidmic ?

@hidmic hidmic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

First pass !

Comment thread rclpy/rclpy/executors.py Resolved
Comment thread rclpy/src/rclpy/action_client.hpp Resolved
Comment thread rclpy/src/rclpy/context.cpp Resolved
Comment thread rclpy/src/rclpy/context.hpp Resolved
Comment thread rclpy/src/rclpy/context.hpp Resolved
Comment thread rclpy/src/rclpy/logging.cpp Resolved
Comment thread rclpy/src/rclpy/node.hpp Resolved
Comment thread rclpy/src/rclpy/node.hpp Resolved
Comment thread rclpy/test/test_destruction.py Outdated
node.destroy_node()
with pytest.raises(InvalidHandle):
node.destroy_node()
# with pytest.raises(InvalidHandle):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ahcorde hmm, doesn't it throw anymore? Can we destroy a node twice?

Comment thread rclpy/test/test_subscription.py Resolved
Signed-off-by: ahcorde <[email protected]>
Comment thread rclpy/src/rclpy/node.cpp Resolved
Comment thread rclpy/src/rclpy/node.hpp Resolved
Comment thread rclpy/test/test_destruction.py Outdated
try:
node = rclpy.create_node('test_node2', context=context)
node.destroy_node()
# with pytest.raises(InvalidHandle):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ahcorde #771 (comment) still holds.

Comment thread rclpy/src/rclpy/node.hpp Resolved
node.destroy_node()
# handle valid because it's still being used
with node.handle:
pass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ahcorde why the change?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

# handle valid because it's still being used

This was an intentional change in #739 with Destroyable. Once someone asks to destroy a Destroyable, it will block new uses of it. The Handle class allowed new uses as long as someone else was already using it, but that could lead to two threads blocking destruction forever as long as their uses overlapped.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That makes total sense. I still think we should test that usage attempts after destruction requests fail.

node.destroy_node()
with pytest.raises(InvalidHandle):
node.destroy_node()
node.destroy_node()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ahcorde #771 (comment) still applies.

Comment thread rclpy/test/test_handle.py
with node.handle:
node.handle.destroy()
with node.handle:
pass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ahcorde why the change?

Comment thread rclpy/src/rclpy/node.cpp Resolved
Comment thread rclpy/src/rclpy/context.cpp Resolved
Comment thread rclpy/rclpy/node.py Outdated
def __del__(self):
self.destroy()

def destroy(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

unnecessary addition?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ahcorde mind removing this method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah weird, the github UI is still showing it for me. I'll look at the new commits locally :-/

Comment thread rclpy/rclpy/node.py Outdated
def handle(self, value):
raise AttributeError('handle cannot be modified after node creation')

def __del__(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems unnecessary. All the python objects on the node will already destroy themselves when they're garbage collected.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But we should call here destroy_node() otherwise the destruction fails

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But we should call here destroy_node() otherwise the destruction fails

What does that mean? What does the failure look like?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ahcorde mind removing this method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread rclpy/src/rclpy/_rclpy_action.cpp Resolved
Comment thread rclpy/src/rclpy/context.cpp Resolved
Comment thread rclpy/src/rclpy/context.cpp Resolved
ahcorde and others added 5 commits April 20, 2021 11:41
Signed-off-by: ahcorde <[email protected]>
Signed-off-by: ahcorde <[email protected]>
This along with an extra call to `destroy_node()` in `Node.__del__` was
causing a segfault in the test_publisher test. The problem is
`destroy_when_not_in_use()` was being called twice, and the second time
is now a problem because rcl_ptrs_ is dereferenced in the qos_event
destroy() method.

Signed-off-by: Shane Loretz <[email protected]>
This should not be required. If it is, it means there's a bug that shows
when the Node is garbage collected.

Signed-off-by: Shane Loretz <[email protected]>
The order of the pointers in the RclPtrs structs is important. Things at
the bottom are destroyed first, so children must be placed underneath
the parent's that must be kept alive.

Signed-off-by: Shane Loretz <[email protected]>
…773)

* Rough pass converting everything

Signed-off-by: Shane Loretz <[email protected]>

* Fixes to get copy destruction order working

Signed-off-by: Shane Loretz <[email protected]>

* Fix qos_event test flakiness

Signed-off-by: Shane Loretz <[email protected]>
Signed-off-by: ahcorde <[email protected]>
@ahcorde
ahcorde force-pushed the ahcorde/pybind11-node branch from f36aaac to 33b848d Compare April 23, 2021 10:43
@ahcorde

ahcorde commented Apr 23, 2021

Copy link
Copy Markdown
Contributor Author
  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@ahcorde

ahcorde commented Apr 23, 2021

Copy link
Copy Markdown
Contributor Author

@sloretz, what about the other functions in rclpy that also take a node as the first argument? for example in names.hpp or graph.cpp. Should I move them to the Node class?

This comment is still pending, what do you think @sloretz ?

@sloretz

sloretz commented Apr 23, 2021

Copy link
Copy Markdown
Contributor

@sloretz, what about the other functions in rclpy that also take a node as the first argument? for example in names.hpp or graph.cpp. Should I move them to the Node class?

I think those are fine actually. I would like to get rid of the _rcl_action.cpp file, but I think I was wrong to say everything should be a method on the Node. Maybe some of the action functions could be moved to names.hpp or graph.hpp themselves?

@ahcorde

ahcorde commented Apr 26, 2021

Copy link
Copy Markdown
Contributor Author
  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@sloretz sloretz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just some nitpicks, otherwise this PR looks pretty good!

Comment thread rclpy/rclpy/context.py Resolved
Comment thread rclpy/src/rclpy/init.cpp Outdated

void
init(py::list pyargs, py::capsule pycontext, size_t domain_id)
init(py::list pyargs, Context & context, size_t domain_id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the idea to move the logic in init(...) into a constructor of Context? That sounds fine to me. I don't think the zero initialized context is used anywhere else.

Comment thread rclpy/src/rclpy/publisher.cpp Resolved
Comment thread rclpy/src/rclpy/subscription.hpp Resolved
Comment thread rclpy/src/rclpy/timer.cpp Resolved
@ahcorde

ahcorde commented Apr 27, 2021

Copy link
Copy Markdown
Contributor Author
  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@ahcorde
ahcorde requested a review from sloretz April 27, 2021 09:22
Comment thread rclpy/rclpy/context.py Resolved
Comment thread rclpy/test/test_init_shutdown.py Resolved
@ahcorde

ahcorde commented Apr 28, 2021

Copy link
Copy Markdown
Contributor Author
  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@sloretz sloretz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for iterating! 🎉

@ahcorde
ahcorde merged commit afdd25e into post_galactic_freeze Apr 28, 2021
@delete-merged-branch
delete-merged-branch Bot deleted the ahcorde/pybind11-node branch April 28, 2021 15:41
sloretz added a commit that referenced this pull request Apr 28, 2021
* Convert Publisher and Subscription to use C++ Classes (#756)

* Convert Publisher and Subscription to C++ Classes

Signed-off-by: ahcorde <[email protected]>

* Fixed destructor issues

Signed-off-by: ahcorde <[email protected]>

* [pub/sub 756 addition] use std::shared_ptr holder type and shared_from_this (#757)

* Use std::shared_ptr holder type

Signed-off-by: Shane Loretz <[email protected]>

* Use shared_from_this for pub/sub

Signed-off-by: Shane Loretz <[email protected]>

* Make Timer keep Clock wrapper

Signed-off-by: Shane Loretz <[email protected]>

* Fixed publisher and subscription destructors

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Fixed docs

Signed-off-by: ahcorde <[email protected]>

Co-authored-by: Shane Loretz <[email protected]>

* Use py::class_ for rcl_action_goal_handle_t (#751)

* Class object

Signed-off-by: Greg Balke <[email protected]>

* Python adjustments

Signed-off-by: Greg Balke <[email protected]>

* Adjustments to server and enum

Signed-off-by: Greg Balke <[email protected]>

* Adjust destructor calls

Signed-off-by: Greg Balke <[email protected]>

* Clear out destructor

Signed-off-by: Greg Balke <[email protected]>

* Do what rclcpp did

Signed-off-by: Greg Balke <[email protected]>

* Remove iostream (used for debugging)

Signed-off-by: Greg Balke <[email protected]>

* Matching upstream changes

Signed-off-by: Greg Balke <[email protected]>

* Convert ActionClient to use C++ classes (#759)

* Convert ActionClient to use C++ classes

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Added convert_from_py to utils

Signed-off-by: ahcorde <[email protected]>

* improved docs

Signed-off-by: ahcorde <[email protected]>

* nits to adapt the code to pybind11

Signed-off-by: ahcorde <[email protected]>

* throw a exception in convert_from_py

Signed-off-by: ahcorde <[email protected]>

* Fixed docs

Signed-off-by: ahcorde <[email protected]>

* Convert ActionServer to use C++ Classes (#766)

* Convert ActionServer to use C++ Classes

Signed-off-by: ahcorde <[email protected]>

* Fixed Test_action_server

Signed-off-by: ahcorde <[email protected]>

* Improved docs

Signed-off-by: ahcorde <[email protected]>

* Improved docs and minor fixes

Signed-off-by: ahcorde <[email protected]>

* Convert WaitSet to use C++ Classes (#769)

* Convert WaitSet to use C++ Classes

Signed-off-by: ahcorde <[email protected]>

* added feedback

Signed-off-by: ahcorde <[email protected]>

* Removed unused structs (#770)

Signed-off-by: ahcorde <[email protected]>

* Convert Guardcondition to use C++ classes (#772)

* Convert Guardcondition to use C++ classes

Signed-off-by: ahcorde <[email protected]>

* make destroy guard condition capsule function private

Signed-off-by: ahcorde <[email protected]>

* Pybind11 action goal handle nitpicks (#767)

* Add GoalEvent back to module

Signed-off-by: Shane Loretz <[email protected]>

* self._goal -> self._goal_handle

Signed-off-by: Shane Loretz <[email protected]>

* Remove unused API

Signed-off-by: Shane Loretz <[email protected]>

Co-authored-by: ahcorde <[email protected]>

* Pybind11 actionserver nitpicks and docblock improvements (#774)

Signed-off-by: ahcorde <[email protected]>

* Convert Node and Context to use C++ Classes (#771)

* convert Node and Context to use C++ Classes

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Implement solution 1

Signed-off-by: ahcorde <[email protected]>

* Prevent double early destruction of destroyable

This along with an extra call to `destroy_node()` in `Node.__del__` was
causing a segfault in the test_publisher test. The problem is
`destroy_when_not_in_use()` was being called twice, and the second time
is now a problem because rcl_ptrs_ is dereferenced in the qos_event
destroy() method.

Signed-off-by: Shane Loretz <[email protected]>

* Remove unnecessary Node.__del__

This should not be required. If it is, it means there's a bug that shows
when the Node is garbage collected.

Signed-off-by: Shane Loretz <[email protected]>

* Fix order of pointers in RclPtrs structs

The order of the pointers in the RclPtrs structs is important. Things at
the bottom are destroyed first, so children must be placed underneath
the parent's that must be kept alive.

Signed-off-by: Shane Loretz <[email protected]>

* Intentionally copy rclpy:: objects instead of having RclPtrs structs (#773)

* Rough pass converting everything

Signed-off-by: Shane Loretz <[email protected]>

* Fixes to get copy destruction order working

Signed-off-by: Shane Loretz <[email protected]>

* Fix qos_event test flakiness

Signed-off-by: Shane Loretz <[email protected]>
Signed-off-by: ahcorde <[email protected]>

* MacOS warning

Signed-off-by: ahcorde <[email protected]>

* added feedback and removed some files

Signed-off-by: ahcorde <[email protected]>

* Create only one context otherwise throw a exception

Signed-off-by: ahcorde <[email protected]>

Co-authored-by: Shane Loretz <[email protected]>
Co-authored-by: Shane Loretz <[email protected]>

Co-authored-by: Alejandro Hernández Cordero <[email protected]>
Co-authored-by: Greg Balke <[email protected]>
Co-authored-by: ahcorde <[email protected]>
sloretz added a commit that referenced this pull request Apr 28, 2021
* convert Node and Context to use C++ Classes

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Implement solution 1

Signed-off-by: ahcorde <[email protected]>

* Prevent double early destruction of destroyable

This along with an extra call to `destroy_node()` in `Node.__del__` was
causing a segfault in the test_publisher test. The problem is
`destroy_when_not_in_use()` was being called twice, and the second time
is now a problem because rcl_ptrs_ is dereferenced in the qos_event
destroy() method.

Signed-off-by: Shane Loretz <[email protected]>

* Remove unnecessary Node.__del__

This should not be required. If it is, it means there's a bug that shows
when the Node is garbage collected.

Signed-off-by: Shane Loretz <[email protected]>

* Fix order of pointers in RclPtrs structs

The order of the pointers in the RclPtrs structs is important. Things at
the bottom are destroyed first, so children must be placed underneath
the parent's that must be kept alive.

Signed-off-by: Shane Loretz <[email protected]>

* Intentionally copy rclpy:: objects instead of having RclPtrs structs (#773)

* Rough pass converting everything

Signed-off-by: Shane Loretz <[email protected]>

* Fixes to get copy destruction order working

Signed-off-by: Shane Loretz <[email protected]>

* Fix qos_event test flakiness

Signed-off-by: Shane Loretz <[email protected]>
Signed-off-by: ahcorde <[email protected]>

* MacOS warning

Signed-off-by: ahcorde <[email protected]>

* added feedback and removed some files

Signed-off-by: ahcorde <[email protected]>

* Create only one context otherwise throw a exception

Signed-off-by: ahcorde <[email protected]>

Co-authored-by: Shane Loretz <[email protected]>
Co-authored-by: Shane Loretz <[email protected]>
sloretz added a commit that referenced this pull request Jun 23, 2021
* convert Node and Context to use C++ Classes

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Implement solution 1

Signed-off-by: ahcorde <[email protected]>

* Prevent double early destruction of destroyable

This along with an extra call to `destroy_node()` in `Node.__del__` was
causing a segfault in the test_publisher test. The problem is
`destroy_when_not_in_use()` was being called twice, and the second time
is now a problem because rcl_ptrs_ is dereferenced in the qos_event
destroy() method.

Signed-off-by: Shane Loretz <[email protected]>

* Remove unnecessary Node.__del__

This should not be required. If it is, it means there's a bug that shows
when the Node is garbage collected.

Signed-off-by: Shane Loretz <[email protected]>

* Fix order of pointers in RclPtrs structs

The order of the pointers in the RclPtrs structs is important. Things at
the bottom are destroyed first, so children must be placed underneath
the parent's that must be kept alive.

Signed-off-by: Shane Loretz <[email protected]>

* Intentionally copy rclpy:: objects instead of having RclPtrs structs (#773)

* Rough pass converting everything

Signed-off-by: Shane Loretz <[email protected]>

* Fixes to get copy destruction order working

Signed-off-by: Shane Loretz <[email protected]>

* Fix qos_event test flakiness

Signed-off-by: Shane Loretz <[email protected]>
Signed-off-by: ahcorde <[email protected]>

* MacOS warning

Signed-off-by: ahcorde <[email protected]>

* added feedback and removed some files

Signed-off-by: ahcorde <[email protected]>

* Create only one context otherwise throw a exception

Signed-off-by: ahcorde <[email protected]>

Co-authored-by: Shane Loretz <[email protected]>
Co-authored-by: Shane Loretz <[email protected]>
Signed-off-by: Shane Loretz <[email protected]>
sloretz added a commit that referenced this pull request Aug 11, 2021
* convert Node and Context to use C++ Classes

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Added feedback

Signed-off-by: ahcorde <[email protected]>

* Implement solution 1

Signed-off-by: ahcorde <[email protected]>

* Prevent double early destruction of destroyable

This along with an extra call to `destroy_node()` in `Node.__del__` was
causing a segfault in the test_publisher test. The problem is
`destroy_when_not_in_use()` was being called twice, and the second time
is now a problem because rcl_ptrs_ is dereferenced in the qos_event
destroy() method.

Signed-off-by: Shane Loretz <[email protected]>

* Remove unnecessary Node.__del__

This should not be required. If it is, it means there's a bug that shows
when the Node is garbage collected.

Signed-off-by: Shane Loretz <[email protected]>

* Fix order of pointers in RclPtrs structs

The order of the pointers in the RclPtrs structs is important. Things at
the bottom are destroyed first, so children must be placed underneath
the parent's that must be kept alive.

Signed-off-by: Shane Loretz <[email protected]>

* Intentionally copy rclpy:: objects instead of having RclPtrs structs (#773)

* Rough pass converting everything

Signed-off-by: Shane Loretz <[email protected]>

* Fixes to get copy destruction order working

Signed-off-by: Shane Loretz <[email protected]>

* Fix qos_event test flakiness

Signed-off-by: Shane Loretz <[email protected]>
Signed-off-by: ahcorde <[email protected]>

* MacOS warning

Signed-off-by: ahcorde <[email protected]>

* added feedback and removed some files

Signed-off-by: ahcorde <[email protected]>

* Create only one context otherwise throw a exception

Signed-off-by: ahcorde <[email protected]>

Co-authored-by: Shane Loretz <[email protected]>
Co-authored-by: Shane Loretz <[email protected]>
Signed-off-by: Shane Loretz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants