Skip to content

Commit c55fc2d

Browse files
authored
Merge pull request #1688 from microsoftgraph/bugfix/stream-deadlock
fix: deadlock for batch request content once it passes a certain size
2 parents 1ed68ec + 3c70728 commit c55fc2d

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/main/java/com/microsoft/graph/core/content/BatchRequestContent.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public BatchRequestContent createNewBatchFromFailedRequests (@Nonnull Map<String
161161
*/
162162
@Nonnull
163163
public InputStream getBatchRequestContent() throws IOException {
164-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
164+
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
165165
JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
166166
writer.beginObject();
167167
writer.name(CoreConstants.BatchRequest.REQUESTS);
@@ -172,11 +172,9 @@ public InputStream getBatchRequestContent() throws IOException {
172172
writer.endArray();
173173
writer.endObject();
174174
writer.flush();
175-
PipedInputStream in = new PipedInputStream();
176-
try(final PipedOutputStream out = new PipedOutputStream(in)) {
177-
outputStream.writeTo(out);
178-
return in;
179-
}
175+
final ByteArrayInputStream out = new ByteArrayInputStream(outputStream.toByteArray());
176+
outputStream.close();
177+
return out;
180178
}
181179
private static final String AUTHORIZATION_HEADER_KEY = "authorization";
182180
private void writeBatchRequestStep(BatchRequestStep requestStep, JsonWriter writer) throws IOException {

src/test/java/com/microsoft/graph/core/requests/BatchRequestBuilderTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.microsoft.graph.core.CoreConstants;
55
import com.microsoft.graph.core.content.BatchRequestContent;
66
import com.microsoft.graph.core.models.BatchRequestStep;
7+
import com.microsoft.kiota.HttpMethod;
78
import com.microsoft.kiota.RequestInformation;
89
import com.microsoft.kiota.authentication.AnonymousAuthenticationProvider;
910
import okhttp3.MediaType;
@@ -12,9 +13,16 @@
1213
import org.junit.jupiter.api.Test;
1314

1415
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.net.URI;
18+
import java.nio.charset.StandardCharsets;
19+
import java.util.ArrayList;
1520
import java.util.Arrays;
21+
import java.util.List;
22+
import java.io.ByteArrayInputStream;
1623

1724
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1826

1927
class BatchRequestBuilderTest {
2028

@@ -36,4 +44,26 @@ void BatchRequestBuilder_DefaultBuilderTest() throws IOException {
3644
assertEquals(client.getRequestAdapter(), batchRequestBuilder.getRequestAdapter());
3745

3846
}
47+
@Test
48+
void BatchContentDoesNotDeadlockOnLargeContent() throws IOException {
49+
final BaseClient client = new BaseClient(new AnonymousAuthenticationProvider(), "https://localhost");
50+
final BatchRequestContent batchRequestContent = new BatchRequestContent(client);
51+
final List<InputStream> streamsToClose = new ArrayList<>();
52+
for (int i = 0; i < 20; i++) {
53+
final RequestInformation requestInformation = new RequestInformation();
54+
requestInformation.httpMethod = HttpMethod.POST;
55+
requestInformation.setUri(URI.create("https://graph.microsoft.com/v1.0/me/"));
56+
final String payload = "{\"displayName\": \"Test\", \"lastName\": \"User\", \"mailNickname\": \"testuser\", \"userPrincipalName\": \"testUser\", \"passwordProfile\": {\"forceChangePasswordNextSignIn\": true, \"password\": \"password\"}, \"accountEnabled\": true}";
57+
final InputStream content = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
58+
streamsToClose.add(content);
59+
requestInformation.setStreamContent(content, CoreConstants.MimeTypeNames.APPLICATION_JSON);
60+
batchRequestContent.addBatchRequestStep(requestInformation);
61+
}
62+
BatchRequestBuilder batchRequestBuilder = new BatchRequestBuilder(client.getRequestAdapter());
63+
RequestInformation requestInformation = batchRequestBuilder.toPostRequestInformation(batchRequestContent);
64+
assertNotNull(requestInformation);
65+
for (final InputStream inputStream : streamsToClose) {
66+
inputStream.close();
67+
}
68+
}
3969
}

0 commit comments

Comments
 (0)