|
50 | 50 | import com.microsoft.identity.common.java.commands.ICommand; |
51 | 51 | import com.microsoft.identity.common.java.commands.InteractiveTokenCommand; |
52 | 52 | import com.microsoft.identity.common.java.commands.parameters.InteractiveTokenCommandParameters; |
| 53 | +import com.microsoft.identity.common.java.exception.BaseException; |
53 | 54 | import com.microsoft.identity.common.java.exception.ClientException; |
54 | 55 | import com.microsoft.identity.common.java.exception.ErrorStrings; |
55 | 56 | import com.microsoft.identity.common.java.flighting.CommonFlight; |
56 | 57 | import com.microsoft.identity.common.java.flighting.CommonFlightsManager; |
57 | 58 | import com.microsoft.identity.common.java.logging.Logger; |
| 59 | +import com.microsoft.identity.common.java.opentelemetry.AttributeName; |
| 60 | +import com.microsoft.identity.common.java.opentelemetry.SpanExtension; |
58 | 61 | import com.microsoft.identity.common.java.util.IPlatformUtil; |
59 | 62 | import com.microsoft.identity.common.java.util.StringUtil; |
60 | 63 |
|
|
67 | 70 | import javax.net.ssl.KeyManagerFactory; |
68 | 71 |
|
69 | 72 | import edu.umd.cs.findbugs.annotations.Nullable; |
| 73 | +import io.opentelemetry.api.trace.Span; |
| 74 | +import io.opentelemetry.api.trace.StatusCode; |
70 | 75 | import lombok.AllArgsConstructor; |
71 | 76 | import lombok.NonNull; |
72 | 77 |
|
@@ -328,4 +333,32 @@ private boolean isValidHubRedirectURIForNAATests(String redirectUri) { |
328 | 333 | || redirectUri.equals("msauth://com.microsoft.teams/fcg80qvoM1YMKJZibjBwQcDfOno=") |
329 | 334 | || redirectUri.equals("https://login.microsoftonline.com/common/oauth2/nativeclient")); |
330 | 335 | } |
| 336 | + |
| 337 | + public void handleShutdownForOutOfMemoryError(@NonNull final BaseException exception, @NonNull final String tag) { |
| 338 | + // When receiving an out of memory error, instead of gracefully returning a failure result, we should shut down |
| 339 | + // current broker process, to allow a new broker process to be launched at the next request from client app. |
| 340 | + // This will result in a new broker process with fresh memory allocation, rather than repeating out of memory |
| 341 | + // errors. In the case that this is an msal-only scenario, and this code is running inside client app, then |
| 342 | + // client app will be shut down and will need to be launched again. |
| 343 | + |
| 344 | + // Log status code and record exception in span |
| 345 | + final Span currentSpan = SpanExtension.current(); |
| 346 | + currentSpan.setStatus(StatusCode.ERROR); |
| 347 | + currentSpan.recordException(exception); |
| 348 | + |
| 349 | + // Attach the stack trace, to debug in telemetry later |
| 350 | + currentSpan.setAttribute(AttributeName.out_of_memory_exception_stacktrace.name(), |
| 351 | + StringUtil.getStacktraceAsStringFromElementArray(exception.getStackTrace())); |
| 352 | + |
| 353 | + // End the span |
| 354 | + currentSpan.end(); |
| 355 | + |
| 356 | + Logger.error(tag, "Received an out of memory error, shutting broker process so a new one can be launched.", exception); |
| 357 | + |
| 358 | + // Shut down current process |
| 359 | + // Calling client app will receive an MsalClientException with "Activity killed unexpectedly" from MSAL if broker is shut down with this statement |
| 360 | + // Calling application should not crash, and should be able to make another call, which will result in a new broker process. |
| 361 | + System.exit(0); |
| 362 | + } |
| 363 | + |
331 | 364 | } |
0 commit comments