Skip to content

Commit db93b21

Browse files
committed
Add assertion to get_local_service
Ensure that the local instance exists and is initialized before accessing it in `get_local_service`.
1 parent 099cf61 commit db93b21

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

include/seastar/core/sharded.hh

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ public:
516516

517517
/// Checks whether the local instance has been initialized.
518518
bool local_is_initialized() const noexcept;
519-
520519
private:
521520
template <typename... Args>
522521
shared_ptr<Service> create_local_service(Args&&... args) {
@@ -525,20 +524,18 @@ private:
525524
return s;
526525
}
527526

527+
/// Performs the same check as `local_is_initialized`, but throws an exception
528+
/// if the local instance is not initialized.
529+
void check_local() const;
530+
528531
shared_ptr<Service> get_local_service() {
529-
auto inst = _instances[this_shard_id()].service;
530-
if (!inst) {
531-
throw no_sharded_instance_exception(pretty_type_name(typeid(Service)));
532-
}
533-
return inst;
532+
check_local();
533+
return _instances[this_shard_id()].service;
534534
}
535535

536536
shared_ptr<const Service> get_local_service() const {
537-
auto inst = _instances[this_shard_id()].service;
538-
if (!inst) {
539-
throw no_sharded_instance_exception(pretty_type_name(typeid(Service)));
540-
}
541-
return inst;
537+
check_local();
538+
return _instances[this_shard_id()].service;
542539
}
543540
};
544541

@@ -916,6 +913,13 @@ bool sharded<Service>::local_is_initialized() const noexcept {
916913
_instances[this_shard_id()].service;
917914
}
918915

916+
template <typename Service>
917+
void sharded<Service>::check_local() const {
918+
if (!local_is_initialized()) {
919+
throw no_sharded_instance_exception(pretty_type_name(typeid(Service)));
920+
}
921+
}
922+
919923
SEASTAR_MODULE_EXPORT_BEGIN
920924
/// \addtogroup smp-module
921925
/// @{

0 commit comments

Comments
 (0)