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

Event API - timeout ignores filters #3714

Open
DragDen opened this issue May 1, 2024 · 1 comment
Open

Event API - timeout ignores filters #3714

DragDen opened this issue May 1, 2024 · 1 comment

Comments

@DragDen
Copy link

DragDen commented May 1, 2024

While spending a couple of days banging my head against the desk I noticed a rather suspicious part in an OpenOS event lib.
In the file /lib/event.lua on the lines 143-150 we have this code:

  repeat
    local signal = table.pack(computer.pullSignal(seconds))
    if signal.n > 0 then
      if not (seconds or filter) or filter == nil or filter(table.unpack(signal, 1, signal.n)) then
        return table.unpack(signal, 1, signal.n)
      end
    end
  until signal.n == 0

The 'seconds' variable here contains the amount of seconds for a timeout.
However, if I understand correctly, the timeout will only lapse if there is not a single event in computer's queue. Regardless of filters.

For example, if we're pulling for a modem_message with a timeout of two seconds, and repeatedly mash a key on the keyboard, the pullEvent will keep waiting indefinitely (until we leave the keyboard alone).

@DragDen
Copy link
Author

DragDen commented May 1, 2024

My suggestion for a fix is something like this:

  local startSeconds = computer.uptime()
  repeat
    local waitTime = seconds - (computer.uptime() - startSeconds)
    local signal = table.pack(computer.pullSignal(waitTime))
    if signal.n > 0 then
      if not (seconds or filter) or filter == nil or filter(table.unpack(signal, 1, signal.n)) then
        return table.unpack(signal, 1, signal.n)
      end
    end
  until signal.n == 0

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

No branches or pull requests

1 participant