UART write loop could lead to wdt with low bitrates or large buffers by dok-net · Pull Request #7799 · esp8266/Arduino

@dok-net

EspSoftwareSerial successfully uses optimistic_yield for the same purpose already.
Fixes #7746

@dok-net

d-a-v

Choose a reason for hiding this comment

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

LGTM !

earlephilhower

Choose a reason for hiding this comment

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

Just worried about yield() in the UART code. Need to be very sure it's safe and not used by SYS to log/etc...

while (size--)
while (size--) {
uart_do_write_char(uart_nr, pgm_read_byte(buf++));
optimistic_yield(10000UL);

Choose a reason for hiding this comment

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

AIUI, optimistic_yield will panic() if not in the CONT context. This would make any SYS-callback printouts (TCP, others) potentially crash.

@devyte, @d-a-v, any comments/ideas? I like the idea, but the forced panic seems worse than a potential WDT...

Choose a reason for hiding this comment

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

optimistic_yield() checks if it can yield (i. e. in CONT), and if enough time has elapsed since the start of the current loop. If true, it yield()s, otherwise does nothing.

So it shouldn't panic.

However: I seem to remember there was a change to its behavior so that yield actually only happens once every blah ms instead of after blah ms since loop start.

In any case:

  • At a glance, I think the argument of 10000 is way too big, I would use something like 500, i. e. yield at most every 0.5s
  • this yield won't help in the case of slow comms attempted from SYS, but you could argue that comms should be done from CONT

Choose a reason for hiding this comment

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

The unit is µs so 10k is 10ms.
It should be fine.
Maybe 1ms is better to sustain high throughput in case of a network transfer.

edit all functions taking time as parameter should have the unit in their names, starting with delay() ... ah it's arduino API, we can't change it :) Our polled time structure have it though. We also could change optimistic_yield() as it is internal API.

devyte

while (size--)
while (size--) {
uart_do_write_char(uart_nr, pgm_read_byte(buf++));
optimistic_yield(10000UL);

Choose a reason for hiding this comment

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

optimistic_yield() checks if it can yield (i. e. in CONT), and if enough time has elapsed since the start of the current loop. If true, it yield()s, otherwise does nothing.

So it shouldn't panic.

However: I seem to remember there was a change to its behavior so that yield actually only happens once every blah ms instead of after blah ms since loop start.

In any case:

  • At a glance, I think the argument of 10000 is way too big, I would use something like 500, i. e. yield at most every 0.5s
  • this yield won't help in the case of slow comms attempted from SYS, but you could argue that comms should be done from CONT

devyte

Choose a reason for hiding this comment

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

Oops, when I took a look before I saw ms. You're right, it's us. Maybe I need glasses...
One yield every 10ms should be ok. It might be worth testing if there's a difference vs 1ms before merging, but I don't see that as a stopper.

Approving.

earlephilhower

Choose a reason for hiding this comment

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

Fair enough, thanks for the clarification. LGTM!