Commit dec8471
runtime: hold ctypes c_char_p byte buffers in Python locals to prevent GC
Lab-observed bug 2026-05-27: model emitted Chinese tokens + random
text fragments after the role-based v1.2.3 refactor. Root cause was
a ctypes lifetime issue — ctypes c_char_p does NOT own the bytes it
points at, so temporaries like role.encode('utf-8') get garbage-
collected before the C side (rkllm_run) finishes reading them.
The pre-refactor code only had ONE c_char_p assignment per call
(input_data.prompt_input), which Python happened to keep alive in
the inp struct via reference. The refactor added a second
(inp.role = role.encode('utf-8')) — both temporaries became
collectable and the C side saw uninitialized memory.
Fix: hold both encoded bytes objects in NAMED Python locals so they
stay reachable for the full duration of the rkllm_run call. Mirrors
the pattern in Rockchip's reference Python example which wraps with
ctypes.c_char_p(...) for the same reason.
55/55 tests pass.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>1 parent 00bf346 commit dec8471
1 file changed
Lines changed: 11 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
470 | 470 | | |
471 | 471 | | |
472 | 472 | | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
473 | 482 | | |
474 | 483 | | |
475 | | - | |
| 484 | + | |
476 | 485 | | |
477 | 486 | | |
478 | | - | |
| 487 | + | |
479 | 488 | | |
480 | 489 | | |
481 | 490 | | |
| |||
0 commit comments