Skip to content

When using multitask with dependent tasks that fail, rake will sometimes exit before all subprocesses have exited #414

@win93

Description

@win93

This behavior can be seen with the following Rakefile, then running rake all:

fake_task_names = ('a'..'z').to_a

multitask all: fake_task_names

fake_task_names.each_with_index do |fake_task_name, i|
  task fake_task_name do
    sh "sleep #{i} && echo 'simulated failure' && false"
  end
end

I expect that rake would let any jobs in flight finish up before reporting failure. Instead it reports failure, but some of the "simulated failure" messages print after rake exits, which ends up printing over my shell prompt.

I dug through the code a little bit and was able to come up with this as a suggested fix:

--- /home/alexg/.gem/ruby/3.0.0/gems/rake-13.0.6/lib/rake/application.rb~       2022-02-06 12:28:46.800753532 -0600
+++ /home/alexg/.gem/ruby/3.0.0/gems/rake-13.0.6/lib/rake/application.rb        2022-02-06 12:28:11.989792288 -0600
@@ -119,15 +119,15 @@
     end
 
     # Run the given block with the thread startup and shutdown.
     def run_with_threads
       thread_pool.gather_history if options.job_stats == :history
 
       yield
-
+    ensure
       thread_pool.join
       if options.job_stats
         stats = thread_pool.statistics
         puts "Maximum active threads: #{stats[:max_active_threads]} + main"
         puts "Total threads in play:  #{stats[:total_threads_in_play]} + main"
       end
       ThreadHistoryDisplay.new(thread_pool.history).show if

This would make sure that we wait on threads in the thread pool, even if an exception was raised from FileUtils#create_shell_runner, for example.

Using rake: 13.0.6, ruby: ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions