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

Progress bar not completing on Windows #8

Closed
mihaiconstantin opened this issue Feb 19, 2023 · 0 comments
Closed

Progress bar not completing on Windows #8

mihaiconstantin opened this issue Feb 19, 2023 · 0 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@mihaiconstantin
Copy link
Owner

mihaiconstantin commented Feb 19, 2023

The following code will fail on Windows.

# Create a specification object.
specification <- Specification$new()

# Set the cluster requirements.
specification$set_cores(2)
specification$set_type("psock")

# Create an asynchronous backend.
backend <- AsyncBackend$new()

# Create a progress-tracking context.
context <- ProgressDecorator$new()

# Create a bar.
bar <- ModernBar$new()

# Set the backend.
context$set_backend(backend)

# Start the backend.
context$start(specification)

# Set the bar.
context$set_bar(bar)

# Configure the bar.
context$configure_bar(show_after = 0)

# Run a task.
context$sapply(1:1000, function(x) { Sys.sleep(0.01); x })

# Get the output.
output <- context$get_output()

# Close the backend.
context$stop()

It most likely has to do with the private method .decorate() in the
ProgressDecorator class. It seems that on Unix systems, the injection

fun_body[[length_fun_body + 1]] <- substitute(
# The injected expression.
on.exit({
cat("\n", file = log, sep = "", append = TRUE)
}),
# The environment to use for substitution.
parent.frame(n = 1)
)

works as expected. However, on Windows, it leads to race conditions. Therefore,
the temporary log file does not contain as many new lines as the tasks that were
executed. As a result, the task finishes executing, but the progress bar is not
updated to reflect this. Then, the function hangs, waiting for the progress bar
to be terminated.

Two possible ways I can think to tackle this:

  1. Ensure the log file is locked while a process writes to it (e.g., see
    filelock package). Will have to understand how much overhead this adds.
    Worst-case scenario, it may only be used for Windows systems.

  2. In the .show_progress() of the ProgressDecorator class, check the task
    state and break out of the loop to update the progress bar if the task
    is finished. This is not desirable, however, as it would mean that the
    progress bar would not reflect the actual progress of the task.

while (tasks_processed < total) {
# Get the current number of tasks processed.
current_tasks_processed <- length(readLines(log, warn = FALSE))
# Redraw the bar only if the number of tasks processed has increased.
if (current_tasks_processed > tasks_processed) {
# Update the number of tasks processed.
tasks_processed <- current_tasks_processed
# Update the progress bar to completed state.
private$.bar$update(tasks_processed)
}
# Wait a bit.
Sys.sleep(timeout)
}

Thanks to @eschat for uncovering this.

@mihaiconstantin mihaiconstantin self-assigned this Feb 19, 2023
@mihaiconstantin mihaiconstantin added bug Something isn't working help wanted Extra attention is needed labels Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
Status: Done
Development

No branches or pull requests

1 participant