-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Process improvements #3079
Process improvements #3079
Conversation
@@ -47,8 +47,12 @@ class Process | |||
nil | |||
end | |||
|
|||
def self.ppid : LibC::PidT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, looks like you moved the ppid
method in the file and the addition and deletion got split between two commits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, made a mistake while committing, fixed.
c2270c3
to
23af70c
Compare
def self.pgid : LibC::PidT | ||
pgid(0) | ||
end | ||
|
||
# Returns the process group identifier of the process with the process | ||
# identifier *pid*. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about:
Returns the process group identifier of a process identified by
pid
.
What about moving the Process instance methods from |
Nice refactor, just a few improvements and it's 👍 to merge from me! |
23af70c
to
df78e95
Compare
If anything I would collapse all of |
I wouldn't mind merging the two files into one |
df78e95
to
4783b29
Compare
end | ||
|
||
private def exec_io_for(io) | ||
# TODO: Compiler bug: The union doesn't properly degrade to IO::FileDescriptior|Bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jhass why is this method needed? If I replace this method body with just io
it continues to compile and run just fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you try make spec
?
Ah actually this is from before I had exec_internal
, if you add type restrictions to it, it resurfaces:
diff --git a/src/process.cr b/src/process.cr
index a0ca02f..29f333a 100644
--- a/src/process.cr
+++ b/src/process.cr
@@ -235,9 +235,9 @@ class Process
argv,
env,
clear_env,
- exec_io_for(fork_input || input),
- exec_io_for(fork_output || output),
- exec_io_for(fork_error || error),
+ fork_input || input,
+ fork_output || output,
+ fork_error || error,
chdir
)
rescue ex
@@ -375,7 +375,7 @@ class Process
end
# :nodoc:
- protected def self.exec_internal(command : String, argv, env, clear_env, input, output, error, chdir)
+ protected def self.exec_internal(command : String, argv, env, clear_env, input : Bool|IO::FileDescriptor, output : Bool|IO::FileDescriptor, error : Bool|IO::FileDescriptor, chdir)
reopen_io(input, STDIN, "r")
reopen_io(output, STDOUT, "w")
reopen_io(error, STDERR, "w")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I can remove the method and leave out the restrictions, working around the bug in another way, or add the restrictions so the method becomes necessary again and serves as a reminder about the bug. What do you prefer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I was able to reproduce it, I'll try to see what's going on. Later I'll report an issue, for now you can leave it as you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jhass Mmm... input
can be nil, the restriction on run
is Stdio
which includes Nil
. So I think the error is correct. Or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's indeed nil
, needs_pipe?
returns true, so fork_input
is taken, I guess the actual bug was that a not_nil!
didn't help either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it brings something back, I can no longer reproduce it complaining about nil
instead of MemoryIO
:
diff --git a/src/process.cr b/src/process.cr
index 5d9bff5..c683032 100644
--- a/src/process.cr
+++ b/src/process.cr
@@ -235,9 +235,9 @@ class Process
argv,
env,
clear_env,
- fork_input || input,
- fork_output || output,
- fork_error || error,
+ (fork_input || input).not_nil!,
+ (fork_output || output).not_nil!,
+ (fork_error || error).not_nil!,
chdir
)
rescue ex
@@ -360,7 +360,7 @@ class Process
end
# :nodoc:
- protected def self.exec_internal(command : String, argv, env, clear_env, input, output, error, chdir)
+ protected def self.exec_internal(command : String, argv, env, clear_env, input : Bool|IO::FileDescriptor, output : Bool|IO::FileDescriptor, error : Bool|IO::FileDescriptor, chdir)
reopen_io(input, STDIN, "r")
reopen_io(output, STDOUT, "w")
reopen_io(error, STDERR, "w")
make spec
errors with:
in ./src/process.cr:233: no overload matches 'Process.exec_internal' with types String, Array(Pointer(UInt8)), Nil, Bool, (IO::FileDescriptor | MemoryIO), IO::FileDescriptor, IO::FileDescriptor, Nil
Overloads are:
- Process.exec_internal(command : String, argv, env, clear_env, input : Bool | IO::FileDescriptor, output : Bool | IO::FileDescriptor, error : Bool | IO::FileDescriptor, chdir)
Couldn't find overloads for these types:
- Process.exec_internal(command : String, argv : Array(Pointer(UInt8)), env : Nil, clear_env : Bool, input : MemoryIO, output : IO::FileDescriptor, error : IO::FileDescriptor, chdir : Nil)
Process.exec_internal(
^~~~~~~~~~~~~
Since casting to a sub-union isn't possible, this was my solution to tell Crystal what the actually possible types are at this point.
diff --git a/src/process.cr b/src/process.cr
index 5d9bff5..e56defa 100644
--- a/src/process.cr
+++ b/src/process.cr
@@ -235,9 +235,9 @@ class Process
argv,
env,
clear_env,
- fork_input || input,
- fork_output || output,
- fork_error || error,
+ (fork_input || input).as(Bool|IO::FileDescriptor),
+ (fork_output || output).as(Bool|IO::FileDescriptor),
+ (fork_error || error).as(Bool|IO::FileDescriptor),
chdir
)
rescue ex
@@ -360,7 +360,7 @@ class Process
end
# :nodoc:
- protected def self.exec_internal(command : String, argv, env, clear_env, input, output, error, chdir)
+ protected def self.exec_internal(command : String, argv, env, clear_env, input : Bool|IO::FileDescriptor, output : Bool|IO::FileDescriptor, error : Bool|IO::FileDescriptor, chdir)
reopen_io(input, STDIN, "r")
reopen_io(output, STDOUT, "w")
reopen_io(error, STDERR, "w")
actually crashes the compiler now that I try it.
Let's drop Our original |
|
Can we then name it |
|
4783b29
to
caecc9c
Compare
caecc9c
to
0a5c550
Compare
I don't think everyone is familiar with UNIX, and having |
# are flushed and closed, all child processes are inherited by PID 1. This does | ||
# not run any handlers registered with `at_exit`, use `::exit` for that. | ||
# | ||
# *status* is the exit status of the current process. | ||
def self.exit(status = 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aparte:
- what about renaming it to
Process.exit!
like in Ruby, to mark the difference and avoid a confusion? ::exit(bool)
would be nice sugar too.
If those are interesting I'll propose a follow-up PR.
This is about familiarity, I brought examples of where |
|
Let's leave it as |
Then this is done from my side :) |
Maybe |
@jhass Thank you! And thank you for also cleaning up the code and documenting it. It's super readable now! |
No description provided.