-
Notifications
You must be signed in to change notification settings - Fork 29
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
Callbacks can execute in wrong order #150
Comments
Looking at the results from the example code above, this is what happens in each test run:
Each test run generates some data. The expected values would be something like this:
Here are some actual results, with the "good" (matching) rows removed, and only the mismatched rows displayed:
I suspect (but don't know for sure) that these mixups happen because of poor time resolution in the Docker container running in the VM, and because there's a bug with the Lines 26 to 33 in 9335226
|
I made a branch which recorded the timestamps of Callback objects as they are created, into a What I found is that the timestamps increase monotonically, except at the locations where the callback executes in the wrong order. For example, notice how these values increase until index 9:
So this means that the problem is that, when the Timestamp objects are created, they don't always have increasing values. This is the code that's run when they're created. It boils down to a call to Lines 7 to 21 in 9096d0c
I wonder if this is due to a bug in the Docker VM. |
It looks like the decreasing Here are some SO questions where the user experienced a similar problem with VirtualBox: The second one links to an issue in the VirtualBox tracker: https://www.virtualbox.org/ticket/7915 |
Here's an example adapted from one of the SO links above, which demonstrates that docker run --rm -ti rocker/shiny /bin/bash
R
install.packages(c('cpp11', 'later', 'brio', 'callr', 'cli', 'decor', 'desc',
'tibble', 'vctrs'))
library(cpp11)
cpp_source(
code = '
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
// Returns time in milliseconds
unsigned long GetTickCount() {
struct timeval start, end;
unsigned long myTickCount;
gettimeofday(&start, NULL);
myTickCount = start.tv_sec*1000 + start.tv_usec/1000;
return myTickCount;
}
[[cpp11::register]]
void time_test() {
int i = 0;
unsigned long start = GetTickCount();
unsigned long a = start;
unsigned long b;
// Run for 20 seconds
while(a - start < 20000) {
a = GetTickCount();
// Sleep for 0.1ms
usleep(100);
b = GetTickCount();
if (a > b) {
printf(
"iteration: %ld, a-start: %ld a: %ld, b:%ld, b-a=%ld\\n",
i, a-start, a, b, b-a
);
}
i++;
}
}
')
time_test()
#> iteration: 93320, a-start: 12493 a: 1624722553189, b:1624722553168, b-a=-21 The result above is what happens in Docker 2.5.0.0 on Mac. After about 12.5 seconds, it recorded an instance where the clock went backward by 21 milliseconds. I think the solution may be to use |
Great find!! Maybe CLOCK_BOOTTIME on Linux? https://stackoverflow.com/a/3527632 |
The Docker blog has a post about how they handle time drift on Mac: As long as |
Originally from rstudio/shiny#3429. I've been able to reproduce this problem on Docker for Mac, using versions 2.5.0.0 and above. Older versions seem fine. Also, I can't seem to reproduce it when running R directly on the Mac, or R on Linux, or R in Docker on Linux.
When the C API is used and tasks are scheduled very quickly, sometimes they do not execute in the correct order.
I've posted example code at https://gist.github.com/wch/d96b3c66644282a312c5df22433d46cd
The text was updated successfully, but these errors were encountered: