Skip to content

RSDK-10305 resources should support close #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/viam/sdk/module/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
Registry::get().lookup_model(cfg.name());
if (reg) {
try {
res->close();
Copy link
Member Author

Choose a reason for hiding this comment

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

@randhid per what you were discussing in slack, if we're trying to reconfigure a modular resource that doesn't implement reconfigure we will now 1) stop it if it's stoppable, 2) call its close method always, and then 3) rebuild it

const std::shared_ptr<Resource> res = reg->construct_resource(deps, cfg);
manager->replace_one(cfg.resource_name(), res);
} catch (const std::exception& exc) {
Expand Down
3 changes: 3 additions & 0 deletions src/viam/sdk/resource/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ Name Resource::get_resource_name() const {
return get_resource_name(kResource);
}

// default behavior is to just return
void Resource::close() {}
Copy link
Member Author

Choose a reason for hiding this comment

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

@randhid this was the implementation detail I wanted to run by you. looking at golang, the default behavior for close is a no-op. is that desired here as well?

Copy link
Member

Choose a reason for hiding this comment

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

Is it true that that is the default behaviour? Mind pointing me to where you are seeing that?

My experience is needing to opt-into the no-op behaviour by embedding a resource.TriviallyCloseable in my resource class, or opting into always rebuild behaviour by including resource.AlwaysRebuild, my understanding hwne having to implement go modules is to have to implement close using those two or a Close implementation, or it just won't build.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh no, I'm mistaken. The default is a no-op for the TriviallyCloseable specifically, you are correct!

by embedding a resource.TriviallyCloseable in my resource class, or opting into always rebuild behaviour by including resource.AlwaysRebuild

I think I'm a little confused by this, how does AlwaysRebuild affect the Close behavior? since a Resource needs to define Close and Close could certainly be called outside the context of a Reconfigure, doesn't including AlwaysRebuild still require us to define Close?

Copy link
Member

Choose a reason for hiding this comment

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

I'm sorry I should have been clearer. I was trying to explain that there are two embeddings available from the resource package: TriviallyCloseable, which provides a no-op closer, and AlwaysRebuild, which forces a close and rebuild flow. You are correct - AlwaysRebuild requires you to implement a Close.


} // namespace sdk
} // namespace viam
3 changes: 3 additions & 0 deletions src/viam/sdk/resource/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class Resource {
/// @brief Return the resource's name.
virtual std::string name() const;

/// @brief Closes a resource
virtual void close();

private:
std::string name_;

Expand Down