-
Notifications
You must be signed in to change notification settings - Fork 273
[yaws_sup] Fix RSS startup deadlock by reordering child specs #504
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
QuinnWilton
wants to merge
2
commits into
erlyaws:master
Choose a base branch
from
QuinnWilton:fix/rss-startup-ordering
base: master
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 1 commit
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
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,165 @@ | ||
| %% Regression test for yaws_rss startup ordering. | ||
| %% | ||
| %% yaws_rss:open/2 is called during yaws_config:load (inside | ||
| %% yaws_server:init/1) when the config file contains an RSS section. | ||
| %% yaws_rss must be registered before yaws_server starts, otherwise | ||
| %% yaws_rss:open/2 blocks for up to 10 seconds then crashes. | ||
| %% | ||
| %% The fix moves yaws_sup_restarts (which starts yaws_rss) before | ||
| %% yaws_server in yaws_sup's child spec list. | ||
| -module(rss_startup_SUITE). | ||
|
|
||
| -include("testsuite.hrl"). | ||
|
|
||
| -compile(nowarn_export_all). | ||
| -compile(export_all). | ||
|
|
||
| all() -> | ||
| [ | ||
| config_load_blocks_without_rss, | ||
| config_load_completes_with_rss, | ||
| sup_starts_rss_before_server | ||
| ]. | ||
|
|
||
| groups() -> | ||
| []. | ||
|
|
||
| %%==================================================================== | ||
| init_per_suite(Config) -> | ||
| _ = application:load(yaws), | ||
| Dir = ?tempdir(?MODULE), | ||
| ok = filelib:ensure_dir(filename:join([Dir, "log", "dummy"])), | ||
| ok = filelib:ensure_dir(filename:join([Dir, "www", "dummy"])), | ||
| ok = filelib:ensure_dir(filename:join([Dir, "rss", "dummy"])), | ||
| ConfPath = write_rss_conf(Dir), | ||
| [{conf_path, ConfPath} | Config]. | ||
|
|
||
| end_per_suite(_Config) -> | ||
| _ = application:unload(yaws), | ||
| ok. | ||
|
|
||
| init_per_group(_Group, Config) -> | ||
| Config. | ||
|
|
||
| end_per_group(_Group, _Config) -> | ||
| ok. | ||
|
|
||
| init_per_testcase(_Test, Config) -> | ||
| Config. | ||
|
|
||
| end_per_testcase(_Test, _Config) -> | ||
| %% Clean up any processes we may have started. | ||
| catch gen_server:stop(yaws_rss), | ||
| ok. | ||
|
|
||
| %%==================================================================== | ||
|
|
||
| %% When yaws_rss is not running, yaws_config:load blocks at the </rss> | ||
| %% tag because yaws_rss:open calls wait_for_server, which polls 20 | ||
| %% times at 500ms intervals. This reproduces the deadlock through the | ||
| %% real code path: yaws_server:init -> yaws_config:load -> yaws_rss:open. | ||
| config_load_blocks_without_rss(Config) -> | ||
| ConfPath = ?config(conf_path, Config), | ||
| Env = #env{debug = false, conf = {file, ConfPath}}, | ||
|
|
||
| %% Ensure yaws_rss is not running. | ||
| undefined = erlang:whereis(yaws_rss), | ||
|
|
||
| Self = self(), | ||
| Pid = spawn_link(fun() -> | ||
| Result = yaws_config:load(Env), | ||
| Self ! {config_load_result, Result} | ||
| end), | ||
|
|
||
| %% Give it 1.5s. If yaws_rss were registered, load would return | ||
| %% near-instantly. The wait_for_server loop runs at 500ms intervals, | ||
| %% so after 1.5s it should still be polling. | ||
| receive | ||
| {config_load_result, _} -> | ||
| ct:fail("yaws_config:load returned without yaws_rss running") | ||
| after 1500 -> | ||
| %% Still blocked — confirms the deadlock. Kill the helper to | ||
| %% avoid waiting the full 10 seconds. | ||
| unlink(Pid), | ||
| exit(Pid, kill), | ||
| ok | ||
| end. | ||
|
|
||
| %% When yaws_rss is running, yaws_config:load gets past the RSS | ||
| %% section without blocking. This is the fixed scenario: | ||
| %% yaws_sup_restarts starts yaws_rss before yaws_server calls | ||
| %% yaws_config:load. | ||
| config_load_completes_with_rss(Config) -> | ||
| ConfPath = ?config(conf_path, Config), | ||
| Env = #env{debug = false, conf = {file, ConfPath}}, | ||
|
|
||
| {ok, _} = yaws_rss:start_link(), | ||
|
|
||
| Self = self(), | ||
| Pid = spawn_link(fun() -> | ||
| Result = yaws_config:load(Env), | ||
| Self ! {config_load_result, Result} | ||
| end), | ||
|
|
||
| %% Config loading should get past yaws_rss:open within 3s. The | ||
| %% result (ok or error) doesn't matter — the point is it returned | ||
| %% instead of blocking in wait_for_server. | ||
| receive | ||
| {config_load_result, _} -> | ||
| ok | ||
| after 3000 -> | ||
| unlink(Pid), | ||
| exit(Pid, kill), | ||
| ct:fail("yaws_config:load blocked with yaws_rss running") | ||
| end. | ||
|
|
||
| %% Verify that yaws_sup's child spec list starts yaws_sup_restarts | ||
| %% (which contains yaws_rss) before yaws_server. This is the fix. | ||
| sup_starts_rss_before_server(_Config) -> | ||
| ChildSpecs = yaws_sup:child_specs(), | ||
| Ids = [element(1, Spec) || Spec <- ChildSpecs], | ||
|
|
||
| RestartsPos = index_of(yaws_sup_restarts, Ids), | ||
| ServerPos = index_of(yaws_server, Ids), | ||
|
|
||
| ?assertNotEqual(false, RestartsPos), | ||
| ?assertNotEqual(false, ServerPos), | ||
|
|
||
| %% yaws_sup_restarts must start before yaws_server. | ||
| ?assert(RestartsPos < ServerPos). | ||
|
|
||
| %%==================================================================== | ||
| %% Helpers | ||
| %%==================================================================== | ||
|
|
||
| %% Write a minimal yaws.conf with an RSS section. Returns the path. | ||
| write_rss_conf(Dir) -> | ||
| LogDir = filename:join(Dir, "log"), | ||
| WwwDir = filename:join(Dir, "www"), | ||
| RssDir = filename:join(Dir, "rss"), | ||
| ConfPath = filename:join(Dir, "yaws.conf"), | ||
| Content = io_lib:format( | ||
| "logdir = ~s~n" | ||
| "~n" | ||
| "<server localhost>~n" | ||
| " port = 8099~n" | ||
| " listen = 127.0.0.1~n" | ||
| " docroot = ~s~n" | ||
| " <rss>~n" | ||
| " rss_id = test_feed~n" | ||
| " rss_dir = ~s~n" | ||
| " </rss>~n" | ||
| "</server>~n", | ||
| [LogDir, WwwDir, RssDir]), | ||
| ok = file:write_file(ConfPath, Content), | ||
| ConfPath. | ||
|
|
||
| index_of(Elem, List) -> | ||
| index_of(Elem, List, 1). | ||
|
|
||
| index_of(_Elem, [], _N) -> | ||
| false; | ||
| index_of(Elem, [Elem | _], N) -> | ||
| N; | ||
| index_of(Elem, [_ | T], N) -> | ||
| index_of(Elem, T, N + 1). | ||
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.
Uh oh!
There was an error while loading. Please reload this page.