Skip to content

Commit 03e1946

Browse files
committed
changes for common
1 parent 584a2aa commit 03e1946

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

common/src/main/java/com/microsoft/identity/common/internal/platform/AndroidPlatformUtil.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@
5050
import com.microsoft.identity.common.java.commands.ICommand;
5151
import com.microsoft.identity.common.java.commands.InteractiveTokenCommand;
5252
import com.microsoft.identity.common.java.commands.parameters.InteractiveTokenCommandParameters;
53+
import com.microsoft.identity.common.java.exception.BaseException;
5354
import com.microsoft.identity.common.java.exception.ClientException;
5455
import com.microsoft.identity.common.java.exception.ErrorStrings;
5556
import com.microsoft.identity.common.java.flighting.CommonFlight;
5657
import com.microsoft.identity.common.java.flighting.CommonFlightsManager;
5758
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;
5861
import com.microsoft.identity.common.java.util.IPlatformUtil;
5962
import com.microsoft.identity.common.java.util.StringUtil;
6063

@@ -67,6 +70,8 @@
6770
import javax.net.ssl.KeyManagerFactory;
6871

6972
import edu.umd.cs.findbugs.annotations.Nullable;
73+
import io.opentelemetry.api.trace.Span;
74+
import io.opentelemetry.api.trace.StatusCode;
7075
import lombok.AllArgsConstructor;
7176
import lombok.NonNull;
7277

@@ -328,4 +333,32 @@ private boolean isValidHubRedirectURIForNAATests(String redirectUri) {
328333
|| redirectUri.equals("msauth://com.microsoft.teams/fcg80qvoM1YMKJZibjBwQcDfOno=")
329334
|| redirectUri.equals("https://login.microsoftonline.com/common/oauth2/nativeclient"));
330335
}
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+
331364
}

common4j/src/main/com/microsoft/identity/common/java/controllers/CommandDispatcher.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ private static CommandResult executeCommand(@SuppressWarnings(WarningType.rawtyp
550550
if (baseException instanceof UserCancelException) {
551551
commandResult = CommandResult.ofNull(CommandResult.ResultStatus.CANCEL,
552552
correlationId);
553+
} else if (baseException.getErrorCode().equals(ClientException.OUT_OF_MEMORY)) { // If we receive an out of memory error
554+
command.getParameters().getPlatformComponents().getPlatformUtil().handleShutdownForOutOfMemoryError(baseException, TAG);
553555
} else {
554556
//Post On Error
555557
commandResult = CommandResult.of(CommandResult.ResultStatus.ERROR, baseException,

common4j/src/main/com/microsoft/identity/common/java/util/IPlatformUtil.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package com.microsoft.identity.common.java.util;
2424

2525
import com.microsoft.identity.common.java.commands.ICommand;
26+
import com.microsoft.identity.common.java.exception.BaseException;
2627
import com.microsoft.identity.common.java.exception.ClientException;
2728
import com.microsoft.identity.common.java.exception.ErrorStrings;
2829

@@ -129,4 +130,7 @@ public interface IPlatformUtil {
129130
*/
130131
@Nullable
131132
List<Map.Entry<String, String>> updateWithAndGetPlatformSpecificExtraQueryParameters(@Nullable List<Map.Entry<String, String>> originalList);
133+
134+
void handleShutdownForOutOfMemoryError(@NonNull final BaseException exception, @NonNull final String tag);
135+
132136
}

common4j/src/testFixtures/java/com/microsoft/identity/common/components/MockPlatformComponentsFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.microsoft.identity.common.java.browser.NoopBrowserSelector;
2828
import com.microsoft.identity.common.java.commands.ICommand;
2929
import com.microsoft.identity.common.java.crypto.IDevicePopManager;
30+
import com.microsoft.identity.common.java.exception.BaseException;
3031
import com.microsoft.identity.common.java.exception.ClientException;
3132
import com.microsoft.identity.common.java.interfaces.IPopManagerSupplier;
3233
import com.microsoft.identity.common.java.interfaces.PlatformComponents;
@@ -188,6 +189,11 @@ public String getPackageNameFromUid(int uid) {
188189
public List<Map.Entry<String, String>> updateWithAndGetPlatformSpecificExtraQueryParameters(@Nullable List<Map.Entry<String, String>> originalList) {
189190
return originalList;
190191
}
192+
193+
@Override
194+
public void handleShutdownForOutOfMemoryError(@NonNull final BaseException exception, @NonNull final String tag) {
195+
throw new UnsupportedOperationException();
196+
}
191197
};
192198

193199
public static final IBrowserSelector NON_FUNCTIONAL_BROWSER_SELECTOR = new NoopBrowserSelector();

0 commit comments

Comments
 (0)