Skip to content

ADFA-2652, ADFA-2582 | Fix NPE during process restoration by initializing Termux singletons#884

Merged
jatezzz merged 2 commits intostagefrom
fix/ADFA-2652-process-restoration-crash
Feb 4, 2026
Merged

ADFA-2652, ADFA-2582 | Fix NPE during process restoration by initializing Termux singletons#884
jatezzz merged 2 commits intostagefrom
fix/ADFA-2652-process-restoration-crash

Conversation

@jatezzz
Copy link
Collaborator

@jatezzz jatezzz commented Jan 26, 2026

Description

This PR fixes critical crashes that occur when the application process is killed by the system (e.g., due to memory pressure or "Don't keep activities" developer setting) and subsequently restored.

In these scenarios, the static singletons for TermuxAppSharedProperties and TermuxShellManager were null, leading to NullPointerException crashes in TermuxActivity (when loading properties) and TermuxService (when accessing session lists). I have added defensive initialization methods (ensureTermuxPropertiesInitialized and ensureTermuxShellManagerInitialized) in onCreate to ensure these components are ready before use.

Details

Crash 1 (Activity):
java.lang.NullPointerException: Attempt to invoke virtual method 'void ...TermuxAppSharedProperties.loadTermuxPropertiesFromDisk()' on a null object reference

image

Crash 2 (Service):
java.lang.NullPointerException: Attempt to read from field 'java.util.List ...TermuxShellManager.mTermuxSessions' on a null object reference

image

Before changes

Screen.Recording.2026-01-26.at.12.16.21.PM.mov

After changes

Screen.Recording.2026-01-26.at.5.04.13.PM.mov

Ticket

Observation

The initialization uses the applicationContext to prevent memory leaks and employs an early-return pattern for cleaner readability.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 26, 2026

Warning

Rate limit exceeded

@jatezzz has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 20 minutes and 5 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

Adds two private initializer helpers in TermuxActivity to ensure TermuxAppSharedProperties and TermuxShellManager singletons are initialized; calls added in onCreate() and onServiceConnected() to avoid null references during restoration and service binding.

Changes

Cohort / File(s) Summary
Activity singleton initialization
termux/termux-app/src/main/java/com/termux/app/TermuxActivity.java
Added import for TermuxShellManager; added private helpers ensureTermuxPropertiesInitialized() and ensureTermuxShellManagerInitialized() that null-check and initialize singletons; invoked these helpers from onCreate() and onServiceConnected() to guard against NPEs during process restore and service bind.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • jomen-adfa

Poem

🐰 I hopped through code at break of day,
I placed two guards to keep bugs away,
On create and bind I softly tread,
So nulls won't jump up from their bed,
A tiny hop — peace now holds sway. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the fix for NullPointerException crashes during process restoration by initializing Termux singletons, directly matching the main change.
Description check ✅ Passed The description provides detailed context about the NPE crashes, the specific issues encountered, and the solution implemented through defensive initialization methods, clearly relating to the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/ADFA-2652-process-restoration-crash

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@termux/termux-app/src/main/java/com/termux/app/TermuxActivity.java`:
- Around line 449-456: The method ensureTermuxPropertiesInitialized() in
TermuxActivity has an extraneous trailing semicolon after its closing brace
which causes a compile error; remove the stray semicolon following the method
block so the class body is valid Java (locate the
ensureTermuxPropertiesInitialized() method and delete the final ";" after its
closing brace).

@jatezzz jatezzz requested a review from a team January 27, 2026 13:20
@jatezzz jatezzz force-pushed the fix/ADFA-2652-process-restoration-crash branch 3 times, most recently from 49e6de3 to f56c797 Compare January 28, 2026 13:09
@jatezzz jatezzz requested a review from Daniel-ADFA January 28, 2026 16:51
@jatezzz jatezzz force-pushed the fix/ADFA-2652-process-restoration-crash branch 4 times, most recently from 6a41f02 to 4af7dcd Compare January 30, 2026 22:08
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@termux/termux-app/src/main/java/com/termux/app/TermuxActivity.java`:
- Around line 211-212: In TermuxService.onCreate(), guard against a null
TermuxShellManager by checking getShellManager() (and/or the field mShellManager
set by ensureTermuxShellManagerInitialized()) before using it; if it is null
either postpone initialization (e.g., schedule a retry/handler) or throw a clear
IllegalStateException with a descriptive message indicating Direct
Boot/CredentialProtectedApplicationLoader timing, so calls that follow
(references to mShellManager) never dereference a null object; update
ensureTermuxShellManagerInitialized()/onCreate() to perform this null-check and
handle the null case explicitly.

@jatezzz jatezzz force-pushed the fix/ADFA-2652-process-restoration-crash branch 4 times, most recently from a6668da to fadfd4c Compare February 4, 2026 14:02
Prevent NPEs in TermuxActivity and TermuxService by initializing properties and shell manager.
@jatezzz jatezzz force-pushed the fix/ADFA-2652-process-restoration-crash branch from 031bf7f to ccf61f0 Compare February 4, 2026 16:18
@jatezzz jatezzz merged commit 5269f3f into stage Feb 4, 2026
2 checks passed
@jatezzz jatezzz deleted the fix/ADFA-2652-process-restoration-crash branch February 4, 2026 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants