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

popen: accept a negative timeout and interpret it as zero #9976

Open
Totktonada opened this issue Apr 24, 2024 · 3 comments
Open

popen: accept a negative timeout and interpret it as zero #9976

Totktonada opened this issue Apr 24, 2024 · 3 comments
Labels
feature A new functionality popen

Comments

@Totktonada
Copy link
Member

Sometimes it is convenient to perform a substraction like deadline - now() and pass the resulting value as a timeout. I would expect the following semantic: if timeout <= 0 then perform an action if it is ready-to-go (would not trigger EWOULDBLOCK and fiber yield). Otherwise, return a timeout error.

popen now interprets any negative timeout value as an error.

tarantool/src/lua/popen.c

Lines 285 to 297 in 927e351

/**
* Extract a timeout value from the Lua stack.
*
* Return -1.0 when error occurs.
*/
static ev_tstamp
luaT_check_timeout(struct lua_State *L, int idx)
{
if (lua_type(L, idx) == LUA_TNUMBER)
return lua_tonumber(L, idx);
/* FIXME: Support cdata<int64_t> and cdata<uint64_t>. */
return -1.0;
}

tarantool/src/lua/popen.c

Lines 1743 to 1745 in 927e351

if (!lua_isnil(L, -1) &&
(timeout = luaT_check_timeout(L, -1)) < 0.0)
goto usage;

The list of affected methods:

ph:wait({timeout = -1})
ph:read({timeout = -1})
ph:write({timeout = -1})
@Totktonada Totktonada added bug Something isn't working popen labels Apr 24, 2024
@sergos sergos removed the bug Something isn't working label Apr 26, 2024
@sergos
Copy link
Contributor

sergos commented Apr 26, 2024

It is definitely not a bug, rather a feature request (semantic change even).
We also didn't come to a resolution how it should behave in this case - it's not clear should a popen call fails with a timeout if it takes longer than proposed timeout to execute. Say, popen.new('...', {timeout=0})

@olegrok
Copy link
Collaborator

olegrok commented Apr 26, 2024

See also #7970

@Totktonada
Copy link
Member Author

@olegrok Thanks for pointing it out!

@sergos OK, let's call it a feature. I don't care much in this case.

popen.new() has no timeout and it possibly shouldn't. I want to clarify that we're talking about calls that wait for some externally produced event: for example, arriving data from another process.

Let's take :read() as an example.

If the call has zero timeout (or a negative timeout), it should return immediately, where it means don't wait for an external event. It is OK to do some (relatively) fast local work like a buffer copying.

Let's assume the data is already there and the call can return immediately with the new data or report a timeout error. In this case, it should return the data.

In other works, zero/negative timeout means "don't wait for data arriving" in this case.

Similarly, it means "don't wait for a free space in the pipe buffer" for :write().

And, finally, it means "don't wait for the process termination" for :wait() (buf it is OK to send a zero signal and look on its result).

@Totktonada Totktonada added the feature A new functionality label Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new functionality popen
Projects
None yet
Development

No branches or pull requests

3 participants