Skip to content

Commit

Permalink
Backport #10540 to jetty-10.0.x Fixed deadlock in class initializat…
Browse files Browse the repository at this point in the history
…ion. (#10545)

* Fixed deadlock in class initialization. (#10540)

* Thread T1 may initialize HttpTester.Message that extends MutableHttpFields, so grabs the lock for the initialization of class MutableHttpFields.
* Thread T2 may initialize HttpFields, so grabs the lock for HttpFields and initializes field EMPTY, which calls new MutableHttpFields.
* To initialize MutableHttpFields, T1 must initialize HttpFields, but sees that its lock is taken and waits.
* To initialize HttpFields, T2 must create an instance and therefore initialize MutableHttpFields, but sees that its lock is taken and waits.
* Deadlock.

The solution is to use another class, EmptyHttpFields, to initialize HttpFields.EMPTY, so that there is no deadlock.

Signed-off-by: Joakim Erdfelt <[email protected]>
Co-authored-by: Simone Bordet <[email protected]>
  • Loading branch information
joakime and sbordet authored Sep 18, 2023
1 parent 3c76f82 commit 43eb08b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.http;

import java.util.Collections;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.stream.Stream;

class EmptyHttpFields extends HttpFields.Immutable
{
public EmptyHttpFields()
{
super(new HttpField[0]);
}

@Override
public Iterator<HttpField> iterator()
{
return Collections.emptyIterator();
}

@Override
public void forEach(Consumer<? super HttpField> action)
{
// no-op
}

@Override
public Stream<HttpField> stream()
{
return Stream.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
*/
public interface HttpFields extends Iterable<HttpField>
{
HttpFields EMPTY = build().asImmutable();
/**
* <p>A constant for an immutable and empty {@link HttpFields}.</p>
*/
HttpFields EMPTY = new EmptyHttpFields();

static Mutable build()
{
Expand Down

0 comments on commit 43eb08b

Please sign in to comment.