apache / apache/maven-surefire

[SUREFIRE-1587] Forked execution prevents correct ServerSocket closing

Open
#2,176 3 comments 0 reactions 0 assignees View on GitHub
bug priority:major
Dominant language
Java
Stars
461
Forks
588
Avg merge
1d 8h
Merged PRs (30d)
19

Description

**[Johannes Wienke](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=languitar)** opened **[SUREFIRE-1587](https://issues.apache.org/jira/browse/SUREFIRE-1587?redirect=false)** and commented

The default mode of forked unit test execution (with JUnit) seems to interfere with socket operations. In our case, we see that with a surefire execution in forked mode, ServerSocket instances, despite returning from a close() call, are sometimes not correctly closed and a subsequent try to acquire a server socket on the same port then fails. This does not happen outside of surefire or when forkMode is set to none.

Here is a simple test case to try this. First the maven project pom.xml:

```

4.0.0

testing
testit
1.0-SNAPSHOT

testit
A simple testit.
http://www.example.com


UTF-8
1.7
1.7



junit
junit
4.11
test




org.apache.maven.plugins
maven-surefire-plugin
2.22.1


```

And here a test file to be placed into the test source tree:

```
package testing;

import org.junit.Test;

import java.net.ServerSocket;

public class AppTest {

@Test
public void testSocketStuff() throws Exception {
while (true) {
System.out.println("Iteration");
final ServerSocket socket = new ServerSocket(55444);
socket.close();
}
}

}
```

Executing this with the default options (forking mode enabled) will pretty soon end up in the following exception after some iterations:

```
[ERROR] testSocketStuff(testing.AppTest) Time elapsed: 1.376 s <<< ERROR!
java.net.BindException: Address already in use (Bind failed)
at testing.AppTest.testSocketStuff(AppTest.java:17)
```

Executing the same test with -DforkMode=none does not result in this failure and the loop runs endlessly.

---

**Affects:** 2.22.1

Contributor guide

Open the contributing guide

Research direction

Start with the provided pom.xml using Maven Surefire 2.22.1 and reproduce the failure in AppTest.testSocketStuff. Compare default forked execution with -DforkMode=none, then trace the forked test execution path. Done means the repeated ServerSocket test no longer fails with BindException under the default forked mode.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
build-system, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.