-
Notifications
You must be signed in to change notification settings - Fork 0
hal: add error handling to radio stats loop #308
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
rslater-cs
wants to merge
2
commits into
dev
Choose a base branch
from
iw-error-handling
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 27 additions & 6 deletions
33
src/services/hal/backends/radio/providers/openwrt/init.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,36 @@ | ||
| local impl = require "services.hal.backends.radio.providers.openwrt.impl" | ||
| local file = require "fibers.io.file" | ||
|
|
||
| ---Check whether OpenWrt UCI is available on this device. | ||
| local function file_exists(path) | ||
| local f, _ = file.open(path, 'r') | ||
| if not f then return false end | ||
| f:close() | ||
| return true | ||
| end | ||
|
|
||
| local function path_has_command(name) | ||
| local path = os.getenv('PATH') or '' | ||
| for dir in path:gmatch('[^:]+') do | ||
| if file_exists(dir .. '/' .. name) then | ||
| return true | ||
| end | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| local backend = {} | ||
|
|
||
| function backend.new(...) | ||
| local impl = require "services.hal.backends.radio.providers.openwrt.impl" | ||
| return impl.new(...) | ||
| end | ||
|
|
||
| ---Check whether OpenWrt UCI and iw are available on this device. | ||
| ---@return boolean | ||
| local function is_supported() | ||
| local f, _ = file.open('/etc/openwrt_release', 'r') | ||
| if f then f:close() return true end | ||
| return false | ||
| return file_exists('/etc/openwrt_release') and path_has_command('iw') | ||
| end | ||
|
|
||
| return { | ||
| is_supported = is_supported, | ||
| backend = impl, | ||
| backend = backend, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/integration/openwrt_vm/tests/test_devicecode_radio_missing_iw_no_hammer.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/env sh | ||
| set -eu | ||
|
|
||
| SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)" | ||
| VM_DIR="$(dirname "$SCRIPT_DIR")" | ||
| ROOT_DIR="$(CDPATH= cd -- "$VM_DIR/../../.." && pwd)" | ||
| SSH="$VM_DIR/scripts/ssh" | ||
| SCP_TO="$VM_DIR/scripts/scp-to" | ||
| REMOTE="/tmp/devicecode-radio-missing-iw-test" | ||
| WORK="$VM_DIR/work/radio-missing-iw-test" | ||
|
|
||
| mkdir -p "$WORK" | ||
|
|
||
| cat > "$WORK/run_devicecode_radio_missing_iw.lua" <<'LUA' | ||
| package.path = table.concat({ | ||
| './src/?.lua', './src/?/init.lua', | ||
| './vendor/lua-fibers/src/?.lua', './vendor/lua-fibers/src/?/init.lua', | ||
| './vendor/lua-bus/src/?.lua', './vendor/lua-bus/src/?/init.lua', | ||
| './vendor/lua-trie/src/?.lua', './vendor/lua-trie/src/?/init.lua', | ||
| package.path, | ||
| }, ';') | ||
|
|
||
| local fibers = require 'fibers' | ||
| local provider = require 'services.hal.backends.radio.provider' | ||
|
|
||
| local function fail(msg) error(msg, 2) end | ||
| local function eq(a, b, msg) | ||
| if a ~= b then | ||
| fail((msg or 'values differ') .. ': expected ' .. tostring(b) .. ', got ' .. tostring(a)) | ||
| end | ||
| end | ||
|
|
||
| fibers.run(function() | ||
| local backend, err = provider.new('radio0') | ||
|
|
||
| eq(backend, nil, 'radio backend should not be selected without iw in PATH') | ||
| assert(tostring(err or ''):match('no supported radio backend'), 'unexpected provider error: ' .. tostring(err)) | ||
| eq(package.loaded['services.hal.backends.radio.providers.openwrt.impl'], nil, | ||
| 'unsupported radio provider should not load the OpenWrt implementation') | ||
| print('devicecode radio missing iw no hammer: ok') | ||
| end) | ||
| LUA | ||
|
|
||
| "$SSH" "rm -rf '$REMOTE'; mkdir -p '$REMOTE'" | ||
| "$SSH" "mkdir -p '$REMOTE/src/services/hal/backends/radio/providers/openwrt' '$REMOTE/vendor/lua-fibers'" | ||
| "$SCP_TO" "$ROOT_DIR/src/services/hal/backends/radio/provider.lua" "$REMOTE/src/services/hal/backends/radio/provider.lua" | ||
| "$SCP_TO" "$ROOT_DIR/src/services/hal/backends/radio/contract.lua" "$REMOTE/src/services/hal/backends/radio/contract.lua" | ||
| "$SCP_TO" "$ROOT_DIR/src/services/hal/backends/radio/providers/openwrt/init.lua" "$REMOTE/src/services/hal/backends/radio/providers/openwrt/init.lua" | ||
| "$SCP_TO" "$ROOT_DIR/vendor/lua-fibers/src" "$REMOTE/vendor/lua-fibers/src" | ||
| "$SCP_TO" "$WORK/run_devicecode_radio_missing_iw.lua" "$REMOTE/run_devicecode_radio_missing_iw.lua" | ||
| "$SSH" "mkdir -p /tmp/devicecode-no-iw-path && lua_bin=\"\$(command -v lua)\" && cd '$REMOTE' && PATH=/tmp/devicecode-no-iw-path \"\$lua_bin\" ./run_devicecode_radio_missing_iw.lua" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| -- tests/unit/hal/radio_driver_spec.lua | ||
|
|
||
| local tests = {} | ||
|
|
||
| local function eq(a, b, msg) | ||
| if a ~= b then | ||
| error((msg or 'assertion failed') .. ': expected ' .. tostring(b) .. ', got ' .. tostring(a), 2) | ||
| end | ||
| end | ||
|
|
||
| function tests.test_stats_loop_stops_when_client_monitor_cannot_start() | ||
| local channel = require 'fibers.channel' | ||
| local runfibers = require 'tests.support.run_fibers' | ||
| local radio = require 'services.hal.drivers.radio' | ||
|
|
||
| runfibers.run(function () | ||
| local start_calls = 0 | ||
| local watch_calls = 0 | ||
| local errors = {} | ||
|
|
||
| local backend = { | ||
| start_client_monitor = function () | ||
| start_calls = start_calls + 1 | ||
| return false, 'failed to start iw event: iw not found' | ||
| end, | ||
|
|
||
| watch_clients_op = function () | ||
| watch_calls = watch_calls + 1 | ||
| error('watch_clients_op should not be called after monitor startup failure', 2) | ||
| end, | ||
| } | ||
|
|
||
| local driver = setmetatable({ | ||
| id = 'radio0', | ||
| cap_emit_ch = channel.new(4), | ||
| iface_update_ch = channel.new(4), | ||
| report_period_ch = channel.new(1), | ||
| backend = backend, | ||
| log = { | ||
| error = function (_, row) | ||
| errors[#errors + 1] = row | ||
| end, | ||
| debug = function () end, | ||
| }, | ||
| }, radio.Driver) | ||
|
|
||
| driver:stats_loop() | ||
|
|
||
| eq(start_calls, 1, 'client monitor should only be started once') | ||
| eq(watch_calls, 0, 'stats loop should not watch clients after monitor startup failure') | ||
| eq(#errors, 1, 'startup failure should be logged once') | ||
| eq(errors[1].what, 'radio_stats_loop_failed', 'failure log event') | ||
| eq(errors[1].id, 'radio0', 'failure log id') | ||
| end, { timeout = 0.1 }) | ||
| end | ||
|
|
||
| function tests.test_stats_loop_stops_when_client_monitor_stream_closes() | ||
| local channel = require 'fibers.channel' | ||
| local op = require 'fibers.op' | ||
| local runfibers = require 'tests.support.run_fibers' | ||
| local radio = require 'services.hal.drivers.radio' | ||
|
|
||
| runfibers.run(function () | ||
| local start_calls = 0 | ||
| local watch_calls = 0 | ||
| local stop_calls = 0 | ||
| local errors = {} | ||
|
|
||
| local backend = { | ||
| start_client_monitor = function () | ||
| start_calls = start_calls + 1 | ||
| return true, '' | ||
| end, | ||
|
|
||
| watch_clients_op = function () | ||
| watch_calls = watch_calls + 1 | ||
| return op.always(nil, 'iw event stream closed') | ||
| end, | ||
|
|
||
| stop_client_monitor_op = function () | ||
| stop_calls = stop_calls + 1 | ||
| return op.always(true, '') | ||
| end, | ||
|
|
||
| terminate = function () return true, nil end, | ||
| } | ||
|
|
||
| local driver = setmetatable({ | ||
| id = 'radio0', | ||
| cap_emit_ch = channel.new(4), | ||
| iface_update_ch = channel.new(4), | ||
| report_period_ch = channel.new(1), | ||
| backend = backend, | ||
| log = { | ||
| error = function (_, row) | ||
| errors[#errors + 1] = row | ||
| end, | ||
| debug = function () end, | ||
| }, | ||
| }, radio.Driver) | ||
|
|
||
| driver:stats_loop() | ||
|
|
||
| eq(start_calls, 1, 'client monitor should only be started once') | ||
| eq(watch_calls, 1, 'closed monitor stream should only be watched once') | ||
| eq(stop_calls, 1, 'started monitor should be stopped after stream failure') | ||
| eq(#errors, 1, 'stream failure should be logged once') | ||
| eq(errors[1].what, 'radio_stats_loop_failed', 'failure log event') | ||
| eq(errors[1].err, 'iw event stream closed', 'failure log err') | ||
| end, { timeout = 0.1 }) | ||
| end | ||
|
|
||
| return tests |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we keep this lazy load inside an error boundary? Previously
implwas loaded inside the provider loaderβspcall(require, path). Thisrequirenow runs outside that protection, so a missing or broken implementation dependency raises fromprovider.new()and can fail the WLAN manager. Please catch the implementation load or constructor failure and return an error instead.