stdin is not guaranteed to be defined#3609
Conversation
In certain constrained environments, stdin will not be defined:
```shell
bash -c $'0>&- python -c \'import sys; print(f"stdin: {sys.stdin}")\''
stdin: None
```
An example of this could be a CI or Deployments environment.
|
Thanks, it sounds reasonable to prevent crashes when stdin is closed. However, what does that mean in term of logic? With your change, there will simply be no config loaded? Have you tested it? Shouldn't we still error out (maybe with a more useful message), as I believe a config is needed for MkDocs to work? Can you also tell us exactly what error you encountered? |
% bash -c 'echo | 0>&- mkdocs serve -f -'
Traceback (most recent call last):
File "/home/pawamoy/.local/bin/mkdocs", line 8, in <module>
sys.exit(cli())
^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/core.py", line 1686, in invoke
sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/core.py", line 943, in make_context
self.parse_args(ctx, args)
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/core.py", line 1408, in parse_args
value, args = param.handle_parse_result(ctx, opts, args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/core.py", line 2400, in handle_parse_result
value = self.process_value(ctx, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/core.py", line 2356, in process_value
value = self.type_cast_value(ctx, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/core.py", line 2344, in type_cast_value
return convert(value)
^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/core.py", line 2316, in convert
return self.type(value, param=self, ctx=ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/types.py", line 83, in __call__
return self.convert(value, param, ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/types.py", line 724, in convert
f, should_close = open_stream(
^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/_compat.py", line 391, in open_stream
return get_binary_stdin(), False
^^^^^^^^^^^^^^^^^^
File "/home/pawamoy/.local/pipx/venvs/mkdocs/lib/python3.11/site-packages/click/_compat.py", line 315, in get_binary_stdin
raise RuntimeError("Was not able to determine binary stream for sys.stdin.")
RuntimeError: Was not able to determine binary stream for sys.stdin.It looks like Click crashes even before MkDocs anyway. |
|
Ah, the case I was talking about was specifically for While click may also seem to suffer from this, the issue I was running into was what prompted this PR. That being said, I think the example you gave made sense, since you were closing stdin before specifically telling click to consume stdin via |
|
Right, thanks a lot! That's clear now 🙂 Yeah, so The |
|
If this is approved can we merge? Would be helpful for a project of mine :) |
pawamoy
left a comment
There was a problem hiding this comment.
Could you actually add a comment above line 365 to say that stdin can sometimes be closed, and also add a test to assert that MkDocs doesn't crash when stdin is closed?
oprypin
left a comment
There was a problem hiding this comment.
Ah well it's a simple enough check.
Btw it's not about stdin being closed, it's about it literally being None. So a bit bizarre that mypy doesn't complain about unchecked usage of sys.stdin.some_attribute
The change makes the following work indeed:
0>&- mkdocs serveLet's move forward with this.. see my comment
In certain constrained environments, stdin will not be defined:
bash -c $'0>&- python -c \'import sys; print(f"stdin: {sys.stdin}")\'' stdin: NoneAn example of this could be a CI or Deployments environment.