-
-
Notifications
You must be signed in to change notification settings - Fork 27
ADFA-4330: Fix TermuxService shellManager NPE on OS service auto-restart #1413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fryanpan
wants to merge
3
commits into
stage
Choose a base branch
from
ADFA-4330-termuxservice-shellmanager-npe
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+73
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
termux/termux-app/src/test/java/com/termux/app/TermuxServiceShellManagerNpeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package com.termux.app; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
|
|
||
| import com.termux.shared.termux.shell.TermuxShellManager; | ||
|
|
||
| import java.lang.reflect.Field; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.robolectric.Robolectric; | ||
| import org.robolectric.RobolectricTestRunner; | ||
| import org.robolectric.android.controller.ServiceController; | ||
|
|
||
| /** | ||
| * Repro for ADFA-4330: when the OS auto-restarts {@link TermuxService} after process death, | ||
| * {@code TermuxApplication.onCreate()} does NOT run, so the static | ||
| * {@link TermuxShellManager} singleton is still {@code null}. | ||
| * | ||
| * <p>On the pre-fix baseline, {@code TermuxService.onCreate()} assigned | ||
| * {@code mShellManager = TermuxShellManager.getShellManager()} (which returns the null | ||
| * singleton), then immediately called {@code runStartForeground() -> buildNotification() -> | ||
| * getTermuxSessionsSize()}, dereferencing the null {@code mShellManager} and throwing a | ||
| * {@link NullPointerException}. | ||
| * | ||
| * <p>The fix changes that assignment to {@code TermuxShellManager.init(applicationContext)}, | ||
| * which lazily creates the singleton, so the service starts cleanly. | ||
| * | ||
| * <p>This test simulates the auto-restart by forcing the static singleton back to {@code null} | ||
| * before creating the service, then asserts the service comes up and | ||
| * {@link TermuxService#getTermuxSessionsSize()} returns 0 instead of NPE-ing. | ||
| */ | ||
| @RunWith(RobolectricTestRunner.class) | ||
| public class TermuxServiceShellManagerNpeTest { | ||
|
|
||
| /** | ||
| * Reset the static singleton to null to mimic a fresh process where | ||
| * TermuxApplication.onCreate() (which would normally call init()) never ran. | ||
| */ | ||
| @Before | ||
| public void clearShellManagerSingleton() throws Exception { | ||
| Field f = TermuxShellManager.class.getDeclaredField("shellManager"); | ||
| f.setAccessible(true); | ||
| f.set(null, null); | ||
| } | ||
|
|
||
| /** Service onCreate() with a null shell-manager singleton must not NPE and must expose usable sessions. */ | ||
| @Test | ||
| public void onCreateWithNullSingleton_doesNotNpe_andSessionsAreUsable() { | ||
| // Sanity: the auto-restart precondition — singleton is null going in. | ||
| assertEquals(null, TermuxShellManager.getShellManager()); | ||
|
|
||
| // Drive the REAL service lifecycle. On stage this throws NullPointerException inside | ||
| // onCreate() -> runStartForeground() -> buildNotification() -> getTermuxSessionsSize(). | ||
| ServiceController<TermuxService> controller = | ||
| Robolectric.buildService(TermuxService.class).create(); | ||
| TermuxService service = controller.get(); | ||
|
|
||
| // After a clean onCreate(), the shell manager must be wired up and queryable. | ||
| assertNotNull("mShellManager must be initialized after onCreate()", | ||
| TermuxShellManager.getShellManager()); | ||
| assertEquals("A freshly created service manages zero sessions", | ||
| 0, service.getTermuxSessionsSize()); | ||
|
|
||
| controller.destroy(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: appdevforall/CodeOnTheGo
Length of output: 268
Add synchronization to
TermuxShellManager.init()to prevent race conditions.The
init()method inTermuxShellManageris unsynchronized and exhibits a classic double-checked locking anti-pattern. IfonCreate()is called concurrently (or from different lifecycle paths), multiple threads could pass theif (shellManager == null)check and create duplicate instances. Addsynchronizedto the method declaration or usevolatilewith a proper synchronized block:🤖 Prompt for AI Agents