handle read from timerfd correctly

When reading the timerfd gives an error, we should return right away
because the timeout did not happen.

If we change the timerfd timeout before reading it, we can get -EAGAIN.
Don't log an error in that case but wait for the new timeout.
This commit is contained in:
Wim Taymans 2022-12-09 17:30:31 +01:00
parent 3a443b4e1c
commit f44d55f6c2
14 changed files with 153 additions and 55 deletions

View file

@ -2428,9 +2428,20 @@ static void alsa_on_timeout_event(struct spa_source *source)
struct state *state = source->data;
snd_pcm_uframes_t delay, target;
uint64_t expire, current_time;
int res;
if (SPA_UNLIKELY(state->started && spa_system_timerfd_read(state->data_system, state->timerfd, &expire) < 0))
spa_log_warn(state->log, "%p: error reading timerfd: %m", state);
if (SPA_LIKELY(state->started)) {
if (SPA_UNLIKELY((res = spa_system_timerfd_read(state->data_system,
state->timerfd, &expire)) < 0)) {
/* we can get here when the timer is changed since the last
* timerfd wakeup, for example by do_reassign_follower() executed
* in the same epoll wakeup cycle */
if (res != -EAGAIN)
spa_log_warn(state->log, "%p: error reading timerfd: %s",
state, spa_strerror(res));
return;
}
}
check_position_config(state);

View file

@ -800,8 +800,14 @@ static void alsa_on_timeout_event(struct spa_source *source)
uint64_t expire;
int res;
if (state->started && spa_system_timerfd_read(state->data_system, state->timerfd, &expire) < 0)
spa_log_warn(state->log, "error reading timerfd: %m");
if (state->started) {
if ((res = spa_system_timerfd_read(state->data_system, state->timerfd, &expire)) < 0) {
if (res != -EAGAIN)
spa_log_warn(state->log, "%p: error reading timerfd: %s",
state, spa_strerror(res));
return;
}
}
state->current_time = state->next_time;

View file

@ -34,6 +34,7 @@
#include <spa/support/loop.h>
#include <spa/utils/list.h>
#include <spa/utils/keys.h>
#include <spa/utils/result.h>
#include <spa/utils/string.h>
#include <spa/node/node.h>
#include <spa/node/utils.h>
@ -348,14 +349,20 @@ static void set_timer(struct impl *this, bool enabled)
}
}
static void read_timer(struct impl *this)
static int read_timer(struct impl *this)
{
uint64_t expirations;
int res = 0;
if (this->async || this->props.live) {
if (spa_system_timerfd_read(this->data_system, this->timer_source.fd, &expirations) < 0)
perror("read timerfd");
if ((res = spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations)) < 0) {
if (res != -EAGAIN)
spa_log_error(this->log, NAME " %p: timerfd error: %s",
this, spa_strerror(res));
}
}
return 0;
}
static int make_buffer(struct impl *this)
@ -369,7 +376,8 @@ static int make_buffer(struct impl *this)
uint32_t filled, avail;
uint32_t index, offset, l0, l1;
read_timer(this);
if (read_timer(this) < 0)
return 0;
if (spa_list_is_empty(&port->empty)) {
set_timer(this, false);

View file

@ -36,6 +36,7 @@
#include <arpa/inet.h>
#include <spa/pod/filter.h>
#include <spa/utils/result.h>
#include <spa/utils/string.h>
#include <spa/support/system.h>
#include <spa/utils/keys.h>
@ -1048,14 +1049,15 @@ static void avb_on_timeout_event(struct spa_source *source)
struct state *state = source->data;
uint64_t expirations, current_time, duration;
uint32_t rate;
int res;
spa_log_trace(state->log, "timeout");
if (spa_system_timerfd_read(state->data_system,
state->timer_source.fd, &expirations) < 0) {
if (errno == EAGAIN)
return;
spa_log_error(state->log, "read timerfd: %m");
if ((res = spa_system_timerfd_read(state->data_system,
state->timer_source.fd, &expirations)) < 0) {
if (res != -EAGAIN)
spa_log_error(state->log, "read timerfd: %s", spa_strerror(res));
return;
}
current_time = state->next_time;

View file

@ -839,11 +839,15 @@ static void media_on_flush_timeout(struct spa_source *source)
{
struct impl *this = source->data;
uint64_t exp;
int res;
spa_log_trace(this->log, "%p: flush on timeout", this);
if (spa_system_timerfd_read(this->data_system, this->flush_timerfd, &exp) < 0)
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
if ((res = spa_system_timerfd_read(this->data_system, this->flush_timerfd, &exp)) < 0) {
if (res != -EAGAIN)
spa_log_warn(this->log, "error reading timerfd: %s", spa_strerror(res));
return;
}
if (this->transport == NULL) {
enable_flush_timer(this, false);
@ -864,12 +868,19 @@ static void media_on_timeout(struct spa_source *source)
uint32_t rate;
struct spa_io_buffers *io = port->io;
uint64_t prev_time, now_time;
int res;
if (this->transport == NULL)
return;
if (this->started && spa_system_timerfd_read(this->data_system, this->timerfd, &exp) < 0)
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
if (this->started) {
if ((res = spa_system_timerfd_read(this->data_system, this->timerfd, &exp)) < 0) {
if (res != -EAGAIN)
spa_log_warn(this->log, "error reading timerfd: %s",
spa_strerror(res));
return;
}
}
prev_time = this->current_time;
now_time = this->current_time = this->next_time;

View file

@ -538,9 +538,13 @@ static void media_on_duplex_timeout(struct spa_source *source)
{
struct impl *this = source->data;
uint64_t exp;
int res;
if (spa_system_timerfd_read(this->data_system, this->duplex_timerfd, &exp) < 0)
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
if ((res = spa_system_timerfd_read(this->data_system, this->duplex_timerfd, &exp)) < 0) {
if (res != -EAGAIN)
spa_log_warn(this->log, "error reading timerfd: %s", spa_strerror(res));
return;
}
set_duplex_timeout(this, this->duplex_timeout);
@ -577,12 +581,18 @@ static void media_on_timeout(struct spa_source *source)
uint64_t exp, duration;
uint32_t rate;
uint64_t prev_time, now_time;
int res;
if (this->transport == NULL)
return;
if (this->started && spa_system_timerfd_read(this->data_system, this->timerfd, &exp) < 0)
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
if (this->started) {
if ((res = spa_system_timerfd_read(this->data_system, this->timerfd, &exp)) < 0) {
if (res != -EAGAIN)
spa_log_warn(this->log, "error reading timerfd: %s", spa_strerror(res));
return;
}
}
prev_time = this->current_time;
now_time = this->current_time = this->next_time;

View file

@ -567,16 +567,19 @@ stop:
enable_flush_timer(this, false);
}
static void sco_on_flush_timeout(struct spa_source *source)
{
struct impl *this = source->data;
uint64_t exp;
int res;
spa_log_trace(this->log, "%p: flush on timeout", this);
if (spa_system_timerfd_read(this->data_system, this->flush_timerfd, &exp) < 0)
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
if ((res = spa_system_timerfd_read(this->data_system, this->flush_timerfd, &exp)) < 0) {
if (res != -EAGAIN)
spa_log_warn(this->log, "error reading timerfd: %s", spa_strerror(res));
return;
}
if (this->transport == NULL) {
enable_flush_timer(this, false);
@ -597,12 +600,18 @@ static void sco_on_timeout(struct spa_source *source)
uint32_t rate;
struct spa_io_buffers *io = port->io;
uint64_t prev_time, now_time;
int res;
if (this->transport == NULL)
return;
if (this->started && spa_system_timerfd_read(this->data_system, this->timerfd, &exp) < 0)
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
if (this->started) {
if ((res = spa_system_timerfd_read(this->data_system, this->timerfd, &exp)) < 0) {
if (res != -EAGAIN)
spa_log_warn(this->log, "error reading timerfd: %s", spa_strerror(res));
return;
}
}
prev_time = this->current_time;
now_time = this->current_time = this->next_time;

View file

@ -34,6 +34,7 @@
#include <spa/support/loop.h>
#include <spa/support/log.h>
#include <spa/support/system.h>
#include <spa/utils/result.h>
#include <spa/utils/list.h>
#include <spa/utils/keys.h>
#include <spa/utils/names.h>
@ -600,12 +601,19 @@ static void sco_on_timeout(struct spa_source *source)
uint64_t exp, duration;
uint32_t rate;
uint64_t prev_time, now_time;
int res;
if (this->transport == NULL)
return;
if (this->started && spa_system_timerfd_read(this->data_system, this->timerfd, &exp) < 0)
spa_log_warn(this->log, "error reading timerfd: %s", strerror(errno));
if (this->started) {
if ((res = spa_system_timerfd_read(this->data_system, this->timerfd, &exp)) < 0) {
if (res != -EAGAIN)
spa_log_warn(this->log, "error reading timerfd: %s",
spa_strerror(res));
return;
}
}
prev_time = this->current_time;
now_time = this->current_time = this->next_time;

View file

@ -32,6 +32,7 @@
#include <spa/support/log.h>
#include <spa/support/loop.h>
#include <spa/utils/names.h>
#include <spa/utils/result.h>
#include <spa/utils/string.h>
#include <spa/node/node.h>
#include <spa/node/keys.h>
@ -180,14 +181,16 @@ static void on_timeout(struct spa_source *source)
struct impl *this = source->data;
uint64_t expirations, nsec, duration;
uint32_t rate;
int res;
spa_log_trace(this->log, "timeout");
if (spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations) < 0) {
if (errno == EAGAIN)
return;
perror("read timerfd");
if ((res = spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations)) < 0) {
if (res != EAGAIN)
spa_log_error(this->log, NAME " %p: timerfd error: %s",
this, spa_strerror(res));
return;
}
nsec = this->next_time;

View file

@ -35,6 +35,7 @@
#include <spa/utils/list.h>
#include <spa/utils/keys.h>
#include <spa/utils/json.h>
#include <spa/utils/result.h>
#include <spa/utils/string.h>
#include <spa/node/node.h>
#include <spa/node/utils.h>
@ -282,14 +283,16 @@ static void on_timeout(struct spa_source *source)
struct impl *this = source->data;
uint64_t expirations, nsec, duration = 10;
uint32_t rate;
int res;
spa_log_trace(this->log, "timeout");
if (spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations) < 0) {
if (errno == EAGAIN)
return;
perror("read timerfd");
if ((res = spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations)) < 0) {
if (res != EAGAIN)
spa_log_error(this->log, NAME " %p: timerfd error: %s",
this, spa_strerror(res));
return;
}
nsec = this->next_time;

View file

@ -215,15 +215,20 @@ static void set_timer(struct impl *this, bool enabled)
}
}
static inline void read_timer(struct impl *this)
static inline int read_timer(struct impl *this)
{
uint64_t expirations;
int res = 0;
if (this->callbacks.funcs || this->props.live) {
if (spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations) < 0)
perror("read timerfd");
if ((res = spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations)) < 0) {
if (res != -EAGAIN)
spa_log_error(this->log, NAME " %p: timerfd error: %s",
this, spa_strerror(res));
}
}
return res;
}
static void render_buffer(struct impl *this, struct buffer *b)
@ -237,7 +242,8 @@ static int consume_buffer(struct impl *this)
struct spa_io_buffers *io = port->io;
int n_bytes;
read_timer(this);
if (read_timer(this) < 0)
return 0;
if (spa_list_is_empty(&port->ready)) {
io->status = SPA_STATUS_NEED_DATA;

View file

@ -233,12 +233,17 @@ static void set_timer(struct impl *this, bool enabled)
static inline void read_timer(struct impl *this)
{
uint64_t expirations;
int res = 0;
if (this->callbacks.funcs || this->props.live) {
if (spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations) < 0)
perror("read timerfd");
if ((res = spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations)) < 0) {
if (res != -EAGAIN)
spa_log_error(this->log, NAME " %p: timerfd error: %s",
this, spa_strerror(res));
}
}
return res;
}
static int make_buffer(struct impl *this)
@ -248,7 +253,8 @@ static int make_buffer(struct impl *this)
struct spa_io_buffers *io = port->io;
int n_bytes;
read_timer(this);
if (read_timer(this) < 0)
return 0;
if (spa_list_is_empty(&port->empty)) {
set_timer(this, false);

View file

@ -33,6 +33,7 @@
#include <spa/support/loop.h>
#include <spa/utils/list.h>
#include <spa/utils/keys.h>
#include <spa/utils/result.h>
#include <spa/utils/string.h>
#include <spa/node/node.h>
#include <spa/node/utils.h>
@ -280,14 +281,20 @@ static void set_timer(struct impl *this, bool enabled)
}
}
static void read_timer(struct impl *this)
static int read_timer(struct impl *this)
{
uint64_t expirations;
int res = 0;
if (this->async || this->props.live) {
if (spa_system_timerfd_read(this->data_system, this->timer_source.fd, &expirations) < 0)
perror("read timerfd");
if ((res = spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations)) < 0) {
if (res != -EAGAIN)
spa_log_error(this->log, NAME " %p: timerfd error: %s",
this, spa_strerror(res));
}
}
return res;
}
static int make_buffer(struct impl *this)
@ -297,7 +304,8 @@ static int make_buffer(struct impl *this)
struct spa_io_buffers *io = port->io;
uint32_t n_bytes;
read_timer(this);
if (read_timer(this) < 0)
return 0;
if (spa_list_is_empty(&port->empty)) {
set_timer(this, false);

View file

@ -267,14 +267,20 @@ static void set_timer(struct impl *this, bool enabled)
}
}
static void read_timer(struct impl *this)
static int read_timer(struct impl *this)
{
uint64_t expirations;
int res = 0;
if (this->async || this->props.live) {
if (spa_system_timerfd_read(this->data_system, this->timer_source.fd, &expirations) < 0)
perror("read timerfd");
if ((res = spa_system_timerfd_read(this->data_system,
this->timer_source.fd, &expirations)) < 0) {
if (res != -EAGAIN)
spa_log_error(this->log, NAME " %p: timerfd error: %s",
this, spa_strerror(res));
}
}
return res;
}
static int make_buffer(struct impl *this)
@ -284,7 +290,8 @@ static int make_buffer(struct impl *this)
uint32_t n_bytes;
int res;
read_timer(this);
if (read_timer(this) < 0)
return 0;
if ((res = spa_vulkan_ready(&this->state)) < 0) {
res = SPA_STATUS_OK;