Skip to content
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

fix: ip_port schema type crash #11077

Merged
merged 5 commits into from Jun 21, 2023

Conversation

zhongwencool
Copy link
Member

@zhongwencool zhongwencool commented Jun 16, 2023

Fixes : Return 500 crash when update bind as a bad port.

2023-06-16T19:14:01.934300+08:00 [error] input-config:, <<"0.0.0.0:188d">>, #{exception => badarg,field => <<"bind">>,path => "root", reason => failed_to_check_field}
2023-06-16T19:14:01.934449+08:00 [error] crasher: initial call: cowboy_stream_h:request_process/3, pid: <0.1183.0>, registered_name: [], error: {#{exception => badarg,field => <<"bind">>,path => "root",reason => failed_to_check_field},[{erlang,list_to_integer,["188d"],[{error_info,#{module => erl_erts_errors}}]},{emqx_schema,to_ip_port,1,[{file,"emqx_schema.erl"},{line,2530}]},{hocon_schema_builtin,convert,2,[{file,"hocon_schema_builtin.erl"},{line,58}]},{hocon_tconf,map_field,4,[{file,"hocon_tconf.erl"},{line,651}]},{hocon_tconf,do_map_union,4,[{file,"hocon_tconf.erl"},{line,784}]},{hocon_tconf,map_field,4,[{file,"hocon_tconf.erl"},{line,599}]},{hocon_tconf,map_one_field_non_hidden,4,[{file,"hocon_tconf.erl"},{line,492}]},{hocon_tconf,map_fields_cont,4,[{file,"hocon_tconf.erl"},{line,431}]},{hocon_tconf,do_map_union,4,[{file,"hocon_tconf.erl"},{line,784}]},{hocon_tconf,map_field,4,[{file,"hocon_tconf.erl"},{line,599}]},{hocon_tconf,map_one_field_non_hidden,4,[{file,"hocon_tconf.erl"},{line,492}]},{hocon_tconf,map_fields_cont,4,[{file,"hocon_tconf.erl"},{line,431}]},{hocon_tconf,map,4,[{file,"hocon_tconf.erl"},{line,301}]},{hocon_tconf,do_check,4,[{file,"hocon_tconf.erl"},{line,267}]},{emqx_dashboard_swagger,check_only,3,[{file,"emqx_dashboard_swagger.erl"},{line,317}]},{emqx_dashboard_swagger,check_request_body,5,[{file,"emqx_dashboard_swagger.erl"},{line,421}]},{emqx_dashboard_swagger,translate_req,3,[{file,"emqx_dashboard_swagger.erl"},{line,305}]},{minirest_handler,do_filter,3,[{file,"/Users/mac/github/emqx/_build/default/lib/minirest/src/minirest_handler.erl"},{line,94}]},{minirest_handler,handle,2,[{file,"/Users/mac/github/emqx/_build/default/lib/minirest/src/minirest_handler.erl"},{line,44}]},{minirest_handler,init,2,[{file,"/Users/mac/github/emqx/_build/default/lib/minirest/src/minirest_handler.erl"},{line,27}]},{cowboy_handler,execute,2,[{file,"cowboy_handler.erl"},{line,41}]},{cowboy_stream_h,execute,3,[{file,"cowboy_stream_h.erl"},{line,318}]},{cowboy_stream_h,request_process,3,[{file,"cowboy_stream_h.erl"},{line,302}]}]}, ancestors: [<0.1182.0>,<0.850.0>,<0.849.0>,ranch_sup,<0.418.0>], message_queue_len: 0, messages: [], links: [<0.1182.0>], dictionary: [], trap_exit: false, status: running, heap_size: 75113, stack_size: 28, reductions: 170532; neighbours:

No crash log after this PR.
image
image
image

The emqx_schema:ip_port() is support integer() format, so we remove the union type.

emqx/hocon#259

Summary

🤖 Generated by Copilot at 1f2e018

This pull request standardizes the schema and validation of listener ports across different modules. It removes the possibility of using integers as ports and replaces it with the ip_port() type. It also refactors and adds some helper functions in emqx_schema.erl.

PR Checklist

Please convert it to a draft if any of the following conditions are not met. Reviewers may skip over until all the items are checked:

  • Added tests for the changes
  • Changed lines covered in coverage report
  • Change log has been added to changes/{ce,ee}/(feat|perf|fix)-<PR-id>.en.md files
  • For internal contributor: there is a jira ticket to track this change
  • If there should be document changes, a PR to emqx-docs.git is sent, or a jira ticket is created to follow up
  • Schema changes are backward compatible

Checklist for CI (.github/workflows) changes

  • If changed package build workflow, pass this action (manual trigger)
  • Change log has been added to changes/ dir for user-facing artifacts update

@zhongwencool zhongwencool requested review from a team, lafirest and JimMoen as code owners June 16, 2023 12:15
thalesmg
thalesmg previously approved these changes Jun 16, 2023
HJianBo
HJianBo previously approved these changes Jun 19, 2023
@@ -1758,7 +1758,7 @@ base_listener(Bind) ->
)},
{"bind",
sc(
hoconsc:union([ip_port(), integer()]),
ip_port(),
#{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove integer: ip_port() also support setting as 1883.

@@ -2418,21 +2418,21 @@ mk_duration(Desc, OverrideMeta) ->
to_duration(Str) ->
case hocon_postprocess:duration(Str) of
I when is_integer(I) -> {ok, I};
_ -> {error, Str}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hocon 0.39.9 has fixed a issue: typerelf:from_string conversion is not executed when value is integer emqx/hocon#259
In order to be compatible with the previous format.

%% {error, "1"} = hocon_postprocess:duration("1")
%% we should ensure:
to_duration("12") -> 12.
to_duration_s("12") -> 12s
to_duration_ms("12") -> 12ms

to_timeout_duration("12") -> 12
to_timeout_duration_ms("12") ->  12ms
to_timeout_duration_s("12") -> 12s

@zhongwencool zhongwencool force-pushed the fix-crash-ip-port-type branch 2 times, most recently from 0b61a01 to 676623b Compare June 19, 2023 12:44
to_integer(Str) when is_list(Str) ->
case string:to_integer(Str) of
{Int, []} -> {ok, Int};
{Int, <<>>} -> {ok, Int};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have another function clause to_integer(Str) when is_binary(Str) -> ... to make this case clause reachable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I will remove is_list

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 5317265419

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 47 unchanged lines in 17 files lost coverage.
  • Overall coverage increased (+0.04%) to 81.767%

Files with Coverage Reduction New Missed Lines %
apps/emqx_bridge_sqlserver/src/emqx_bridge_sqlserver_connector.erl 1 80.56%
apps/emqx_gateway_exproto/src/emqx_exproto_gcli.erl 1 64.15%
apps/emqx_management/src/emqx_mgmt_util.erl 1 13.79%
apps/emqx/src/bpapi/emqx_bpapi.erl 1 95.83%
apps/emqx/src/emqx_broker.erl 1 88.2%
apps/emqx/src/emqx_logger_textfmt.erl 1 75.0%
apps/emqx/src/emqx_message.erl 1 94.83%
apps/emqx/src/emqx_sys_mon.erl 1 91.67%
apps/emqx_ctl/src/emqx_ctl.erl 2 93.26%
apps/emqx_dashboard/src/emqx_dashboard_monitor.erl 2 64.83%
Totals Coverage Status
Change from base Build 5312787191: 0.04%
Covered Lines: 29880
Relevant Lines: 36543

💛 - Coveralls

Copy link
Member

@HJianBo HJianBo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhongwencool zhongwencool merged commit f50d733 into emqx:master Jun 21, 2023
130 checks passed
@zhongwencool zhongwencool deleted the fix-crash-ip-port-type branch June 21, 2023 09:37
@id
Copy link
Collaborator

id commented Jul 4, 2023

Do we need a changelog entry for this one @zhongwencool ?

@zhongwencool
Copy link
Member Author

Do we need a changelog entry for this one @zhongwencool ?

@id OK, I add changelog for this PR #11192

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants