Skip to content

Commit 112cc5c

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 112cc5c

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

include/seastar/core/sharded.hh

+15-10
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ public:
517517
/// Checks whether the local instance has been initialized.
518518
bool local_is_initialized() const noexcept;
519519

520+
/// Performs the same check as `local_is_initialized`, but throws an exception
521+
/// if the local instance is not initialized.
522+
void check_local() const;
523+
520524
private:
521525
template <typename... Args>
522526
shared_ptr<Service> create_local_service(Args&&... args) {
@@ -526,19 +530,13 @@ private:
526530
}
527531

528532
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;
533+
check_local();
534+
return _instances[this_shard_id()].service;
534535
}
535536

536537
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;
538+
check_local();
539+
return _instances[this_shard_id()].service;
542540
}
543541
};
544542

@@ -916,6 +914,13 @@ bool sharded<Service>::local_is_initialized() const noexcept {
916914
_instances[this_shard_id()].service;
917915
}
918916

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

0 commit comments

Comments
 (0)