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

[#518] Replace group leader when using stdio transport #521

Merged
merged 1 commit into from
Mar 6, 2020

Conversation

jfacorro
Copy link
Contributor

@jfacorro jfacorro commented Mar 5, 2020

Description

Replace group leader when using stdio transport.

Fixes #518.

Copy link
Contributor

@alanz alanz left a comment

Choose a reason for hiding this comment

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

LGTM

I saw in reading that the group leader is abused to harvest processes when shutting down. Will that be compromised by this? Will it matter, given we shut down the O/S process as a whole when terminating the server?

@jfacorro
Copy link
Contributor Author

jfacorro commented Mar 6, 2020

I saw in reading that the group leader is abused to harvest processes when shutting down. Will that be compromised by this?

I don't think it will. The processes that wouldn't be killed are the ones that are not under the supervision tree, as long as every process we spawn is linked to a process in the supervision tree it should be fine.

Will it matter, given we shut down the O/S process as a whole when terminating the server?

I agree that it ultimately does not matter for the users. If by any chance some dangling process is still alive when the application is stopped, this could affect some of the tests, depending on what the process does. But this would still happen without the change in this PR. @robertoaloi was having some issues with the process that keeps the TCP connection in his indexing PR.

@jfacorro jfacorro merged commit 688699c into master Mar 6, 2020
@jfacorro jfacorro deleted the 518-restricted-standard-output-access branch March 6, 2020 11:08
@alanz
Copy link
Contributor

alanz commented Mar 6, 2020

Running this under emacs, I get

LSP :: Connected to [erlang-ls:14246 status:starting].
LSP :: erlang-ls:14246 initialized successfully
Error processing message (lsp-unknown-message-type #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8125 data ("error" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8125 data ("code" -32001 "message" "Unexpected error while initialized")) "id" nil "jsonrpc" "2.0"))).

Bisecting blames 9050e6a

@robertoaloi
Copy link
Member

If possible, I would appreciate @dszoboszlay 's input on this one.

@alanz
Copy link
Contributor

alanz commented Mar 6, 2020

BTW, the error in emacs is complaining about this line getting hit

, message => <<"Unexpected error while ", Method/binary>>

@alanz
Copy link
Contributor

alanz commented Mar 6, 2020

My setup is mac, catalina, emacs GNU Emacs 26.3 (build 1, x86_64-apple-darwin18.2.0, NS appkit-1671.20 Version 10.14.3 (Build 18D109)) of 2019-09-02

Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe] [dtrace]

@jfacorro
Copy link
Contributor Author

jfacorro commented Mar 6, 2020

I found the problem! We are using application:get_application() which is relying on the group_leader():

-spec get_application() -> 'undefined' | {'ok', Application} when
      Application :: atom().
get_application() -> 
    application_controller:get_application(group_leader()).

@@ -40,6 +40,8 @@ init([]) ->
, period => 60
},
{ok, Transport} = application:get_env(erlang_ls, transport),
%% Restrict access to stdio when using that transport
(Transport =/= els_stdio) andalso restrict_stdio_access(),

Choose a reason for hiding this comment

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

Isn't this the opposite of what you want? You call restrict_stdio_access() for every transport except for els_stdio.

Copy link
Member

Choose a reason for hiding this comment

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

It looks like @dszoboszlay is correct. I also suggest we do not rely on the andalso semantics for this, since it makes the code less readable. I would rather:

restrict_stdio_access(els_stdio) ->
  restrict_stdio_access();
restrict_stdio_access(_) ->
  ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦

Copy link
Contributor Author

@jfacorro jfacorro Mar 6, 2020

Choose a reason for hiding this comment

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

Fixed by #532

@dszoboszlay
Copy link

All in all, I don't think you should base your solution on changing the group_leader in one supervisor.

  • It prevents application:get_application() from working properly.
  • If you start a different application with a supervisor tree, this solution will not block that application's processes from writing to the standard output.

I'd recommend taking a completely different approach:

  • Start Erlang with the -nouser command line argument. This will tell the kernel not to start a user process, basically blocking access to the standard_io I/O device.
  • You can still access the standard output of the OS process by opening {fd, 0, 1} as a port:
P = erlang:open_port({fd, 0, 1}, []),
erlang:port_command(P, <<"hello world!\n">>),
erlang:port_close(P).

It is very unlikely that any other library would accidentally write to this port! 😄

erszcz added a commit to erszcz/erlang_ls that referenced this pull request Jun 16, 2023
This version brings approx. 30 new PRs. The highlights are:

- Improve map exhaustiveness checking [erlang-ls#524](josefs/Gradualizer#524) by @xxdavid
- Fix all remaining self-check errors [erlang-ls#521](josefs/Gradualizer#521) by @erszcz
- Fix intersection-typed function calls with union-typed arguments [erlang-ls#514](josefs/Gradualizer#514) by @erszcz
- Experimental constraint solver [erlang-ls#450](josefs/Gradualizer#450) by @erszcz
erszcz added a commit to erszcz/erlang_ls that referenced this pull request Jun 16, 2023
This version brings approx. 30 PRs. The highlights are:

- Improve map exhaustiveness checking [erlang-ls#524](josefs/Gradualizer#524) by @xxdavid
- Fix all remaining self-check errors [erlang-ls#521](josefs/Gradualizer#521) by @erszcz
- Fix intersection-typed function calls with union-typed arguments [erlang-ls#514](josefs/Gradualizer#514) by @erszcz
- Experimental constraint solver [erlang-ls#450](josefs/Gradualizer#450) by @erszcz
erszcz added a commit to erszcz/erlang_ls that referenced this pull request Jun 16, 2023
This version brings approx. 30 PRs. The highlights are:

- Improve map exhaustiveness checking [erlang-ls#524](josefs/Gradualizer#524) by @xxdavid
- Fix all remaining self-check errors [erlang-ls#521](josefs/Gradualizer#521) by @erszcz
- Fix intersection-typed function calls with union-typed arguments [erlang-ls#514](josefs/Gradualizer#514) by @erszcz
- Experimental constraint solver [erlang-ls#450](josefs/Gradualizer#450) by @erszcz
robertoaloi pushed a commit that referenced this pull request Jun 17, 2023
This version brings approx. 30 PRs. The highlights are:

- Improve map exhaustiveness checking [#524](josefs/Gradualizer#524) by @xxdavid
- Fix all remaining self-check errors [#521](josefs/Gradualizer#521) by @erszcz
- Fix intersection-typed function calls with union-typed arguments [#514](josefs/Gradualizer#514) by @erszcz
- Experimental constraint solver [#450](josefs/Gradualizer#450) by @erszcz
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.

Restrict the processes that can write to standard output when using stdio transport
4 participants