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

libsubprocess: improve read API #5965

Merged

Conversation

chu11
Copy link
Member

@chu11 chu11 commented May 13, 2024

Problem: The flux_subprocess_read() family of functions return a pointer to a buffer on success and length of buffer in an
optional parameter.

This API style isn't the most intuitive as it is quite different than the read(2) system call. Most notably when EOF
is reached on a read stream you have to check if the buffer pointer is non-NULL and the length returned from an optional parameter is 0.

Solution: Update the read family of functions to return the length from the function and the buffer pointer in an optional parameter.

Update all callers accordingly.


side notes

notes, b/c so much code already used ints and char *s for these functions, I elected to keep int vs ssize_t and char * vs void *.

Relatively simple refactoring ... sorry but the review will be a little tedious, lots of adjustments everywhere :-)

@garlick
Copy link
Member

garlick commented May 14, 2024

We probably shouldn't mix the internal refactoring, external API change, and variable renames in one big commit.

Also, are we sure it is necessary to change the signature of every read function? I was only advocating for changing flux_subprocess_read() in light of the fact that the returned buffer might not be NULL terminated in the future (the unbuffered case). The others (_read_line(), _read_trimmed_line(), _getline() seem to me to be more like fgets() and returning a NULL terminated string result seems natural.

@chu11
Copy link
Member Author

chu11 commented May 14, 2024

We probably shouldn't mix the internal refactoring, external API change, and variable renames in one big commit.

I could splice out the variable renames, but the internal refactoring sort of comes with the change because ...

Also, are we sure it is necessary to change the signature of every read function?

I did this because A) the internal code of all the functions shares the same internals B) as I was working on the UNBUF_LOCAL change, I realized it's awkward to return a pointer on success when there is no line to be read. You're just trusting the user to not read anything from the pointer. As an example, I really disliked a lot of these calls

if (!(ptr = flux_subprocess_read_line (..., &len)))
    error
if (ptr && len == 0)
   ...

vs now it's just

if ((len = flux_subprocess_read_line (..., &ptr)) < 0)
    error
if (len == 0)
   ...

which i like better.

🤷

@chu11 chu11 force-pushed the issue5953_libsubprocess_update_functions branch from 3061932 to 7be8321 Compare May 14, 2024 18:20
@chu11
Copy link
Member Author

chu11 commented May 14, 2024

re-pushed, spliting out the variable renames, it does clean up the big commit alot

@garlick
Copy link
Member

garlick commented May 14, 2024

Well, you make a good point!

Copy link
Member

@garlick garlick left a comment

Choose a reason for hiding this comment

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

Thanks! I'm not spotting anything wrong.
Let's get an ACK from @grondo on the API change before merging though.

@@ -244,7 +244,7 @@ static void stdio_cb (flux_subprocess_t *p, const char *stream)
const char *line;
Copy link
Member

Choose a reason for hiding this comment

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

commit message and PR title: I'd probably call this "improve read API" since it's really about the API change not refactoring.

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 point, re-pushed with that tweaked commit message

@chu11 chu11 changed the title libsubprocess: refactor read API libsubprocess: improve read API May 14, 2024
@chu11 chu11 force-pushed the issue5953_libsubprocess_update_functions branch from 7be8321 to f6d512b Compare May 14, 2024 19:44
Copy link

codecov bot commented May 14, 2024

Codecov Report

Attention: Patch coverage is 98.50746% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 83.32%. Comparing base (9f5ad97) to head (f6d512b).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5965      +/-   ##
==========================================
- Coverage   83.33%   83.32%   -0.02%     
==========================================
  Files         515      515              
  Lines       83398    83396       -2     
==========================================
- Hits        69502    69487      -15     
- Misses      13896    13909      +13     
Files Coverage Δ
src/broker/runat.c 79.65% <100.00%> (ø)
src/cmd/flux-exec.c 76.39% <100.00%> (ø)
src/cmd/flux-start.c 83.21% <100.00%> (ø)
src/cmd/job/mpir.c 85.24% <100.00%> (ø)
src/common/libsubprocess/server.c 78.67% <100.00%> (ø)
src/modules/cron/task.c 77.87% <100.00%> (ø)
src/modules/job-exec/bulk-exec.c 77.86% <100.00%> (ø)
src/modules/job-ingest/worker.c 75.96% <100.00%> (ø)
src/modules/job-manager/plugins/perilog.c 80.13% <100.00%> (ø)
src/shell/output.c 71.86% <100.00%> (ø)
... and 2 more

... and 10 files with indirect coverage changes

Copy link
Contributor

@grondo grondo left a comment

Choose a reason for hiding this comment

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

This seems like an improvement to me!

@@ -244,7 +244,7 @@ static void stdio_cb (flux_subprocess_t *p, const char *stream)
const char *line;
Copy link
Contributor

Choose a reason for hiding this comment

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

Commit message nit: extra space after Solution:

@chu11 chu11 force-pushed the issue5953_libsubprocess_update_functions branch from f6d512b to 7dafb65 Compare May 16, 2024 19:10
chu11 added 3 commits May 16, 2024 12:12
Problem: An error message in subprocess_standard_output() indicated
the wrong function.

Correct the function name in the error message.
Problem: In the near future the API for several libsubprocess functions
will change.  As a result, some variable names that were previously
used may not be the most useful/meaningful afterwards.

In calls to flux_subprocess_read() and similar functions, use the
variable "buf" instead of the generic "ptr" when referring the
buffer of data returned.  Use the variable "len" instead of "lenp"
for the data length.
Problem: The flux_subprocess_read() family of functions
return a pointer to a buffer on success and length of buffer in an
optional parameter.

This API style isn't the most intuitive as it is quite different
than the read(2) system call.  Most notably when EOF
is reached on a read stream you have to check if the buffer pointer
is non-NULL and the length returned from an optional parameter
is 0.

Solution: Update the read family of functions to return the
length from the function and the buffer pointer in an optional parameter.

Update all callers accordingly.

Fixes flux-framework#5953
@chu11 chu11 force-pushed the issue5953_libsubprocess_update_functions branch from 7dafb65 to c436b85 Compare May 16, 2024 19:18
@chu11
Copy link
Member Author

chu11 commented May 16, 2024

thanks, rebased & re-pushed after fixing that commit message nit. will set MWP

@mergify mergify bot merged commit 7bd3e35 into flux-framework:master May 16, 2024
33 checks passed
@chu11 chu11 deleted the issue5953_libsubprocess_update_functions branch May 16, 2024 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants