Native crash (SIGSEGV in getSuggestionsNative) when typing with a multilingual secondary language

Cerrado
#759 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
android, cpp, kotlin
Área
mobile

Línea de trabajo

Start with DictionaryFacilitatorImpl.getSuggestionResults(), where secondary-dictionary queries run in scope.launch and are awaited for 120 ms, then BinaryDictionary.getSuggestions(), where only getTraverseSession() is synchronized and the native call is not. The work is to confirm that overlapping queries share one DicTraverseSession and then make access to that session safe; the reporter already has a candidate fix. Done means no SIGSEGV under overlapping queries, which needs a device or a concurrency reproduction.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Describe the bug
With a secondary language added via Multilingual typing (German + English (US), both main dictionaries installed), the keyboard process sometimes crashes in native code (SIGSEGV, null pointer dereference in libjni_latinime.so). For us it happened right after tapping the clipboard suggestion and when typing fast.

Backtrace (shortened):

F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x11 in tid 10828 (DefaultDispatch)
F DEBUG   : Cause: null pointer dereference
F DEBUG   :   #00..#04 libjni_latinime.so
F DEBUG   :   #11 com.android.inputmethod.latin.BinaryDictionary.access$getSuggestionsNative
F DEBUG   :   #24 com.android.inputmethod.latin.BinaryDictionary.getSuggestions
F DEBUG   :   #30 helium314.keyboard.latin.dictionary.ReadOnlyBinaryDictionary.getSuggestions
F DEBUG   :   #35 helium314.keyboard.latin.dictionary.DictionaryCollection.getSuggestions
F DEBUG   :   #38 helium314.keyboard.latin.DictionaryFacilitatorImpl.getSuggestions
F DEBUG   :   #49 helium314.keyboard.latin.DictionaryFacilitatorImpl$getSuggestionResults$1.invokeSuspend
F DEBUG   :   #53 kotlinx.coroutines.DispatchedTask.run

Cause (from reading the code)
DictionaryFacilitatorImpl.getSuggestionResults() queries the secondary dictionary groups in scope.launch {} on a background thread and waits for them only up to await(120, TimeUnit.MILLISECONDS) (reduced from 500 ms in 05023da). If a query takes longer, the next keystroke starts a new query on the same dictionary while the previous one is still running.

Both queries use the same DicTraverseSession (same session id). Its Java buffers (mInputCodePoints, mPrevWordCodePointArrays, ...) and the native session are shared. BinaryDictionary.getSuggestions() only synchronizes the session lookup in getTraverseSession(), not the native call, so two threads can run getSuggestionsNative() on the same native session at the same time.

Fix that works for us
Hold a lock on the session for the whole query in BinaryDictionary.getSuggestions():

val session = getTraverseSession(sessionId)
synchronized(session) {
    // fill the session buffers, call getSuggestionsNative(..., session.getSession(), ...), read the results
}

(and pass session.getSession() instead of calling getTraverseSession(sessionId) a second time). We haven't seen the crash since. I can send a PR once we've tested it a bit longer.

To Reproduce

  1. Languages & Layouts → German → Multilingual typing → add English (US). Install the main dictionaries for both.
  2. Type fast, or copy some text and tap the clipboard suggestion right away.
  3. Occasionally the keyboard crashes and restarts. It depends on timing, so it doesn't happen every time.

Expected behavior
No crash.

App version & Flavor
Found in GlassType, my fork based on LeanType 4.3.0 (d634e26), standard flavor. BinaryDictionary.getSuggestions() is unchanged in current main (0e673e1), and getSuggestionResults() now waits even shorter (120 ms).

Device Info

  • Model: Samsung Galaxy S23 Ultra (SM-S918B)
  • OS version: Android 17
Lenguaje dominante
Kotlin
Estrellas
1k
Forks
59
Merge medio
14 h 9 min
PR fusionados (30 d)
56

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de LeanBitLab/LeanType

Todos los issues de LeanBitLab/LeanType

Issues similares

Más issues de Kotlin

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.