Skip to content

Commit

Permalink
Use default shutdown time for gen_lsp_server
Browse files Browse the repository at this point in the history
Erlang/OTP `supervisor` documentation says: [1]

> It is also allowed to set it to `infinity`, if the child process is a
> worker.
>
> Warning
>
> Be careful when setting the shutdown time to `infinity` when the child
> process is a worker. Because, in this situation, the termination of
> the supervision tree depends on the child process, it must be
> implemented in a safe way and its cleanup procedure must always
> return.

Therefore, the shutdown time of `gen_lsp_server` is changed from
`infinity` to default value, that is `5000` milliseconds for worker
processes.

Additionally, a sketch of the application supervision tree is added to
module header of `vscode_lsp_app` for easier understanding.

The `-behaviour` attributes are added to the corresponding modules, also
for easier understanding.

[1] https://www.erlang.org/doc/man/supervisor.html
  • Loading branch information
Kornel Horvath committed Nov 21, 2024
1 parent 2a7b0e7 commit 787d006
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 33 deletions.
12 changes: 7 additions & 5 deletions apps/erlangbridge/src/gen_lsp_config_server.erl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
-module(gen_lsp_config_server).

-behavior(gen_server).
-export([start_link/0]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
%% API
-export([start_link/0]).
-export([standard_modules/0, bifs/0]).
-export([update_config/2, root/0, tmpdir/0, codeLensEnabled/0, includePaths/0, linting/0,
verbose/0, autosave/0, proxy/0, search_files_exclude/0, search_exclude/0,
formatting_line_length/0, inlayHintsEnabled/0, verbose_is_include/1]).
verbose/0, autosave/0, proxy/0, search_files_exclude/0, search_exclude/0,
formatting_line_length/0, inlayHintsEnabled/0, verbose_is_include/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-include("lsp_log.hrl").
-define(SERVER, ?MODULE).
Expand Down
7 changes: 6 additions & 1 deletion apps/erlangbridge/src/gen_lsp_config_sup.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
-module(gen_lsp_config_sup).
-behaviour(supervisor).

-export([init/1, start_link/0]).
%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
Expand Down
6 changes: 5 additions & 1 deletion apps/erlangbridge/src/gen_lsp_doc_server.erl
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
-module(gen_lsp_doc_server).

-behavior(gen_server).

%% API
-export([start_link/0]).

-export([document_opened/2, document_changed/2, document_closed/1, opened_documents/0, get_document_contents/1, parse_document/1]).
-export([project_file_added/1, project_file_changed/1, project_file_deleted/1]).
-export([get_syntax_tree/1, get_dodged_syntax_tree/1, get_references/1, get_inlayhints/1]).
-export([root_available/0, config_change/0, project_modules/0, get_module_file/1, get_module_files/1, get_build_dir/0, find_source_file/1]).

%% gen_server callbacks
-export([init/1,handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-include("./lsp_log.hrl").

-define(SERVER, ?MODULE).
Expand Down
7 changes: 6 additions & 1 deletion apps/erlangbridge/src/gen_lsp_doc_sup.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
-module(gen_lsp_doc_sup).
-behaviour(supervisor).

-export([init/1, start_link/0]).
%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
Expand Down
6 changes: 4 additions & 2 deletions apps/erlangbridge/src/gen_lsp_help_server.erl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
-module(gen_lsp_help_server).

-behavior(gen_server).

%% API
-export([start_link/0]).
-export([get_help/2,get_help/5, render_help_body/3]).

%% gen_server callbacks
-export([init/1,handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([get_help/2,get_help/5, render_help_body/3]).

-define(SERVER, ?MODULE).

Expand Down
7 changes: 6 additions & 1 deletion apps/erlangbridge/src/gen_lsp_help_sup.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
-module(gen_lsp_help_sup).
-behaviour(supervisor).

-export([init/1, start_link/0]).
%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
Expand Down
7 changes: 3 additions & 4 deletions apps/erlangbridge/src/gen_lsp_server.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-module(gen_lsp_server).

-behavior(gen_server).

%inspired from https://github.com/kevinlynx/erlang-tcpserver/blob/master/test/test.erl
Expand All @@ -9,12 +8,12 @@
% http://learnyousomeerlang.com/buckets-of-sockets


%API
%% API
-export([start_link/1, start_link/2]).
-export([lsp_log/2, send_to_client/2]).

%export for gen_server
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([lsp_log/2, send_to_client/2]).

-define(SERVER, ?MODULE).
%state
Expand Down
13 changes: 10 additions & 3 deletions apps/erlangbridge/src/gen_lsp_sup.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
-module(gen_lsp_sup).
-behaviour(supervisor).

%% API
-export([start_link/1]).

%% Supervisor callbacks
-export([init/1]).

-define(TCP_OPTIONS, [binary, {packet, raw}, {active, once}, {reuseaddr, true}]).
Expand All @@ -17,8 +21,11 @@ start_link(Port) ->

init(VsCodePort) ->
{ok, LSock} = gen_tcp:listen(list_to_integer(VsCodePort), ?TCP_OPTIONS),
UserSpec = {gen_lsp_server, {gen_lsp_server, start_link, [list_to_integer(VsCodePort), LSock]},
temporary, infinity, worker, [gen_lsp_server]},
UserSpec = #{id => gen_lsp_server,
start => {gen_lsp_server, start_link, [list_to_integer(VsCodePort), LSock]},
restart => temporary,
modules => [gen_lsp_server],
type => worker},
StartSpecs = {{simple_one_for_one, 60, 3600}, [UserSpec]},
gen_tcp:send(LSock, <<"{}">>),
gen_tcp:send(LSock, <<"{}">>),
{ok, StartSpecs}.
53 changes: 44 additions & 9 deletions apps/erlangbridge/src/vscode_lsp_app.erl
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
%%%-------------------------------------------------------------------
%%% @doc Erlang Language Server Process (LSP) for Visual Studio Code.
%%%
%%% Application supervision tree:
%%%
%%% ```
%%% vscode_lsp_entry
%%% |
%%% | vscode_lsp [app.src]
%%% |
%%% vscode_lsp_app [app]
%%% |
%%% vscode_lsp_app_sup [sup]
%%% |
%%% +--gen_lsp_sup [sup]
%%% | |
%%% | +--gen_lsp_server [gen_server]
%%% |
%%% +--gen_lsp_doc_sup [sup]
%%% | | - (D)ETS document_contents
%%% | | - (D)ETS document_inlayhints
%%% | | - (D)ETS dodged_syntax_tree
%%% | | - ETS references
%%% | | - (D)ETS syntax_tree
%%% | |
%%% | +--gen_lsp_doc_server [gen_server]
%%% |
%%% +--gen_lsp_config_sup [sup]
%%% | |
%%% | +--gen_lsp_config_server [gen_server]
%%% |
%%% +--gen_lsp_help_sup [sup]
%%% |
%%% +--gen_lsp_help_server [gen_server]
%%% '''
%%%
%%% @end
%%%-------------------------------------------------------------------
-module(vscode_lsp_app).

-behavior(application).

%% Application callbacks
-export([start/2, stop/1]).


get_port() ->
case init:get_argument(vscode_port) of
{ok, [[P]]} -> P;
_ -> "0"
{ok, [[P]]} -> P;
_ -> "0"
end.


start(_Type, _Args) ->
application:start(inets),
%uncomment to monitor erlang processes
%spawn(fun() -> observer:start() end),
Port = get_port(),
case vscode_lsp_app_sup:start_link(Port) of
{ok, Pid} ->
{ok, Pid};
_Any -> _Any
{ok, Pid} -> {ok, Pid};
_Any -> _Any
end.

stop(_State) ->
ok.
ok.
15 changes: 9 additions & 6 deletions apps/erlangbridge/src/vscode_lsp_app_sup.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
-module(vscode_lsp_app_sup).
-behaviour(supervisor).

-export([init/1, start_link/1, start_sup_socket/1, start_sup_doc/0, start_sup_config/0, start_sup_help/0, start_child/1]).
%% API
-export([start_link/1, start_sup_socket/1, start_sup_doc/0, start_sup_config/0, start_sup_help/0, start_child/1]).

%% Supervisor callbacks
-export([init/1]).

start_link(VsCodePort) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, VsCodePort).
Expand All @@ -20,17 +25,12 @@ socket_spec(VsCodePort) ->
modules => [gen_lsp_sup],
type => supervisor}.

start_sup_socket(VsCodePort) ->
supervisor:start_child({local, ?MODULE}, socket_spec(VsCodePort)).

doc_spec() ->
#{id => gen_lsp_doc_sup,
start => {gen_lsp_doc_sup, start_link, []},
restart => permanent,
modules => [gen_lsp_doc_sup],
type => supervisor}.
% {gen_lsp_doc_sup, {gen_lsp_doc_sup, start_link, []},
% permanent, infinity, supervisor, [gen_lsp_doc_sup]}.

config_spec() ->
#{id => gen_lsp_config_sup,
Expand All @@ -46,6 +46,9 @@ help_spec() ->
modules => [gen_lsp_help_sup],
type => supervisor}.

start_sup_socket(VsCodePort) ->
supervisor:start_child({local, ?MODULE}, socket_spec(VsCodePort)).

start_sup_doc() ->
supervisor:start_child(?MODULE, doc_spec()).

Expand Down

0 comments on commit 787d006

Please sign in to comment.