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

bugfix: io module: ensured read() resumes read in all modes upon EINTR. #17

Closed

Conversation

thibaultcha
Copy link
Member

@thibaultcha thibaultcha commented Jan 16, 2018

src/lib_io.c Outdated
}

static int io_file_readline(lua_State *L, FILE *fp, MSize chop)
{
MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0;
char *buf;
MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0, s = 0;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think using the name s for size values is a good idea. It looks like string :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the name to size

@yangshuxin
Copy link

I don't spot logic error, but the code layout can be improved. IIRC, in some static compilers, when it compiles if-then-else construct, if there is no annotation indicating which target is more frequent, it will assume then-clause is more frequent. This PR just make static compiler make wrong decision. Maybe you can use LJ_UNLIKELY to improve the code layout.

src/lib_io.c Outdated
if (errno == EINTR) { clearerr(fp); continue; }
return 0;
} else {
setnilV(L->top++);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it is no need to set the L->top to nil, as the L->top will be set to nil later if the return value is zero.
https://github.com/openresty/luajit2/blob/v2.1-agentzh/src/lib_io.c#L230

Remember to increase L->top by one once the return value is zero. Another option is to remove the if (!ok) check and set L->top to nil immediately in all branches.

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 catch - I try to minimize the impact of this patch on the upstream source as much as possible. The upstream patch (see LuaJIT/LuaJIT#376) has other improvements to the IO module, but this patch (against the OpenResty fork) focuses solely on EINTR errors. I do not think it is its role to fix other types of behavior if it means the patch size will increase more than needed.

I'll definitely add this to the upstream patch though!

Copy link
Member Author

Choose a reason for hiding this comment

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

(That said, this is my vision of the patch but I am pretty flexible about it, if it is deemed best :) )

@thibaultcha
Copy link
Member Author

thibaultcha commented Jan 16, 2018

@yangshuxin In most (all?) cases in this PR, the error branch comes first, and the error-free branch comes second (then-clause). If what you say is true, I am not sure which branches you are referring to?
I also see no use of LJ_(UN)LIKELY anywhere in the IO module, but I can nonetheless add them.

@thibaultcha
Copy link
Member Author

@yangshuxin Branch prediction builtin added, thanks for the suggestion. I added them with a rule of thumb to only try to optimize the error-free branch of each function.

@agentzh
Copy link
Member

agentzh commented Apr 19, 2018

We now both agree this is not a good idea.

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.

None yet

4 participants