Skip to content

Commit

Permalink
[MNG-7487] Fix deadlock during forked lifecycle executions
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed May 30, 2022
1 parent 88a03f8 commit 0b79a9e
Showing 1 changed file with 23 additions and 14 deletions.
Expand Up @@ -85,6 +85,8 @@ public class MojoExecutor

private final ReadWriteLock aggregatorLock = new ReentrantReadWriteLock();

private final Map<Thread, MojoDescriptor> mojos = new ConcurrentHashMap<>();

public MojoExecutor()
{
}
Expand Down Expand Up @@ -206,10 +208,7 @@ private void execute( MavenSession session, MojoExecution mojoExecution, Project
}
}

try ( ProjectLock lock = new ProjectLock( session, mojoDescriptor, aggregatorLock ) )
{
doExecute( session, mojoExecution, projectIndex, dependencyContext );
}
doExecute( session, mojoExecution, projectIndex, dependencyContext );
}

/**
Expand All @@ -220,13 +219,14 @@ private void execute( MavenSession session, MojoExecution mojoExecution, Project
* TODO: ideally, the builder should take care of the ordering in a smarter way
* TODO: and concurrency issues fixed with MNG-7157
*/
private static class ProjectLock implements AutoCloseable
private class ProjectLock implements AutoCloseable
{
final Lock acquiredAggregatorLock;
final Lock acquiredProjectLock;

ProjectLock( MavenSession session, MojoDescriptor mojoDescriptor, ReadWriteLock aggregatorLock )
ProjectLock( MavenSession session, MojoDescriptor mojoDescriptor )
{
mojos.put( Thread.currentThread(), mojoDescriptor );
if ( session.getRequest().getDegreeOfConcurrency() > 1 )
{
boolean aggregator = mojoDescriptor.isAggregator();
Expand Down Expand Up @@ -254,6 +254,7 @@ public void close()
{
acquiredAggregatorLock.unlock();
}
mojos.remove( Thread.currentThread() );
}

@SuppressWarnings( { "unchecked", "rawtypes" } )
Expand Down Expand Up @@ -292,8 +293,23 @@ private void doExecute( MavenSession session, MojoExecution mojoExecution, Proje

ensureDependenciesAreResolved( mojoDescriptor, session, dependencyContext );

eventCatapult.fire( ExecutionEvent.Type.MojoStarted, session, mojoExecution );
try ( ProjectLock lock = new ProjectLock( session, mojoDescriptor ) )
{
doExecute2( session, mojoExecution );
}
finally
{
for ( MavenProject forkedProject : forkedProjects )
{
forkedProject.setExecutionProject( null );
}
}
}

private void doExecute2( MavenSession session, MojoExecution mojoExecution )
throws LifecycleExecutionException
{
eventCatapult.fire( ExecutionEvent.Type.MojoStarted, session, mojoExecution );
try
{
try
Expand All @@ -314,13 +330,6 @@ private void doExecute( MavenSession session, MojoExecution mojoExecution, Proje

throw e;
}
finally
{
for ( MavenProject forkedProject : forkedProjects )
{
forkedProject.setExecutionProject( null );
}
}
}

public void ensureDependenciesAreResolved( MojoDescriptor mojoDescriptor, MavenSession session,
Expand Down

0 comments on commit 0b79a9e

Please sign in to comment.