termux / termux/termux-api

termux-location -p gps -r updates crashes on second location update and always exits after ~30 seconds

Open
#898 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
4.3k
Forks
920
PR merge metrics
No merged PRs in 30d

Description

Problem description

I'm trying to understand the intended behavior of termux-location -p gps -r updates.

When running:
termux-location -p gps -r updates

the command prints the first location fix, waits about 20–30 seconds, then exits.

Looking at LocationAPI.java, it appears that requestLocationUpdates() is called, but the looper is stopped after 30 seconds:

manager.requestLocationUpdates(provider, 5000, 50.f, listener, null);
... Thread.sleep(30 * 1000);
looper.quit();

Is this the intended behavior of -r updates, or is it expected to provide continuous location updates?

Additionally, if a second location update is delivered before the 30-second timeout (for example, by moving more than the configured 50 m), the Termux app crashes with:
IllegalStateException: JSON must have only one top-level value

This produces an "Error in ResultReturner" notification and temporarily disrupts location delivery. In my testing, concurrent termux-location -p gps -r once polling also missed fixes for a few seconds, resulting in a visible gap in the recorded GPS track.

From the code, it appears that multiple location updates are written using the same JsonWriter, causing the second top-level JSON value to throw the exception.

Steps to reproduce

  1. Run:
    termux-location -p gps -r updates
  2. Move more than approximately 50 m within 30 seconds (e.g. while driving or cycling).
  3. Observe that Termux crashes with:
    IllegalStateException: JSON must have only one top-level value

Expected behavior

Either:

  1. -r updates is intended to stream location updates, in which case it should continue delivering updates without crashing, or
  2. if the 30-second lifetime is intentional, the command should still not crash when a second update is delivered during that period.

Additional information

  • termux-api application version: 0.59.1-1 (F-Droid)
  • termux-api package version (installed through apt): 0.59.1
  • Android OS version: 13
  • Device model: Samsung Galaxy S21

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with LocationAPI.java and reproduce termux-location -p gps -r updates on the reported Android setup. Inspect how requestLocationUpdates, the looper timeout, and the shared JsonWriter handle a second fix. Done means the command no longer crashes on a second update and the intended 30-second or continuous-update behavior is established and verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, cli, mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.