From 8dd49393f2a4e2cbd92738b6928f3a55d93448a6 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Thu, 22 Oct 2026 14:04:42 +0300 Subject: [PATCH] Exit with code 1 instead of aborting when the OSD fails to open its devices Blockstore errors at startup, such as a data device that cannot be opened, escaped from the etcd callback that creates the blockstore, and configuration errors escaped from osd_t construction. vitastor-osd then died with "terminate called after throwing std::runtime_error" and a core dump. Print the error and exit with code 1, so that a startup error can be told from a crash. Other exceptions still abort. --- .gitea/workflows/test.yml | 18 ++++++++++++++++++ src/osd/osd.cpp | 11 ++++++++++- src/osd/osd_main.cpp | 22 +++++++++++++++------- tests/run_tests.sh | 2 ++ tests/test_osd_open_error.sh | 14 ++++++++++++++ 5 files changed, 59 insertions(+), 8 deletions(-) create mode 100755 tests/test_osd_open_error.sh diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index 6c72c970..b13e92a4 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -1913,6 +1913,24 @@ jobs: echo "" done + test_osd_open_error: + runs-on: ubuntu-latest + needs: build + container: ${{env.TEST_IMAGE}}:${{github.sha}} + steps: + - name: Run test + id: test + timeout-minutes: 3 + run: /root/vitastor/tests/test_osd_open_error.sh + - name: Print logs + if: always() && steps.test.outcome == 'failure' + run: | + for i in /root/vitastor/testdata/*.log /root/vitastor/testdata/*.txt; do + echo "-------- $i --------" + cat $i + echo "" + done + test_enospc: runs-on: ubuntu-latest needs: build diff --git a/src/osd/osd.cpp b/src/osd/osd.cpp index 8cd8a134..6932c30e 100644 --- a/src/osd/osd.cpp +++ b/src/osd/osd.cpp @@ -236,7 +236,16 @@ void osd_t::init_blockstore(std::function on_init) if (!json_is_true(this->config["disable_blockstore"])) { auto bs_cfg = json_to_string_map(this->config); - this->bs = bs_factory(bs_cfg); + try + { + this->bs = bs_factory(bs_cfg); + } + catch (std::exception & e) + { + fprintf(stderr, "[OSD %ju] %s\n", this->osd_num, e.what()); + force_stop(1); + return; + } // Pre-configure pool PG shards for (auto & pool_item: st_cli->pool_config) { diff --git a/src/osd/osd_main.cpp b/src/osd/osd_main.cpp index 9491c1ca..ab18f273 100644 --- a/src/osd/osd_main.cpp +++ b/src/osd/osd_main.cpp @@ -67,13 +67,21 @@ int main(int narg, char *args[]) ring_loop_t *ringloop = new ring_loop_t(RINGLOOP_DEFAULT_SIZE); epoll_manager_t *epmgr = new epoll_manager_t(ringloop); auto st_cli = new etcd_state_client_http_t(epmgr->tfd); - osd = new osd_t( - config, ringloop, epmgr->tfd, std::unique_ptr(st_cli), - [ringloop, tfd = epmgr->tfd](blockstore_config_t & cfg) - { - return blockstore_i::create(cfg, ringloop, tfd); - } - ); + try + { + osd = new osd_t( + config, ringloop, epmgr->tfd, std::unique_ptr(st_cli), + [ringloop, tfd = epmgr->tfd](blockstore_config_t & cfg) + { + return blockstore_i::create(cfg, ringloop, tfd); + } + ); + } + catch (std::exception & e) + { + fprintf(stderr, "%s\n", e.what()); + return 1; + } osd->start(); while (1) { diff --git a/tests/run_tests.sh b/tests/run_tests.sh index c29ef29b..ef25facb 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -149,6 +149,8 @@ OLD=1 ./test_resize_auto.sh ./test_osd_tags.sh +./test_osd_open_error.sh + ./test_enospc.sh SCHEME=xor ./test_enospc.sh IMMEDIATE_COMMIT=1 ./test_enospc.sh diff --git a/tests/test_osd_open_error.sh b/tests/test_osd_open_error.sh new file mode 100755 index 00000000..b9df1e1c --- /dev/null +++ b/tests/test_osd_open_error.sh @@ -0,0 +1,14 @@ +#!/bin/bash -ex + +# An OSD that cannot open its data device must exit with code 1, not abort with a core dump + +. `dirname $0`/common.sh + +wait_etcd + +code=0 +build/src/osd/vitastor-osd --osd_num 1 --data_device ./testdata/bin/missing --etcd_address $ETCD_URL &> ./testdata/osd1.log || code=$? +grep -q 'Failed to open data device' ./testdata/osd1.log || format_error "OSD did not report the open error" +[[ $code -eq 1 ]] || format_error "OSD exited with code $code instead of 1" + +format_green OK -- 2.47.3