[iOS][Android] New backend: disposing one RiveFile removes a referenced font from every other live file with the same asset name

未关闭
#461 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
android, ios, react-native, typescript
领域
backend, mobile

调研方向

Read ios/new/AssetLoader.swift, the Android AssetLoader.kt under android/src/new, and the referenced runtime behavior in command_server.cpp. Trace how registering and releasing shared assets affects live files, including the font-handle lifetime on Android; then check the same behavior for images and audio. Done means disposing one file leaves assets available to other live files, and registration does not temporarily clear them.

由索引模型根据 Issue 内容生成。

描述

Description

With the new (default) runtime backend, every asset passed through referencedAssets is registered on the shared worker globally under its name. When a file is disposed, it unregisters that name. The worker applies a name's asset to every loaded file, and unregistering the name clears it in all of them. So when two files reference the same out-of-band font (the same .riv, or two .riv files exported with the same font asset), unmounting one leaves the other with no font: its text runs draw nothing, while its shapes still draw.

We hit this in a production app. A Rive speech bubble stays mounted on the home screen. A reward screen that uses the same referenced font is pushed over it and then popped. Text set on the bubble afterwards never draws.

Environment

  • @rive-app/react-native 0.5.11, new backend (no USE_RIVE_LEGACY). The same code is on main and in 0.5.13, whose only changes are the Android SDK bumps.
  • rive-ios 6.29.0, rive-android 11.14.3
  • React Native 0.86.2, Expo SDK 57, react-native-nitro-modules 0.35.10
  • Reproduced on the iOS 26.5 simulator (iPhone 17 Pro, Release build). Seen on Android too.
  • Web (@rive-app/react-webgl2 with a per-file assetLoader) is not affected.

Reproduction

Take a .riv with a text run whose font is exported as Referenced (not embedded). FONT below is the asset's unique name, as rive-gen-types prints it (for example Inter-4228759).

import { useState } from 'react';
import { Button, View } from 'react-native';
import { RiveView, useRiveFile } from '@rive-app/react-native';

const FONT = 'Inter-4228759';
const ASSETS = { [FONT]: { source: require('./Inter.ttf'), type: 'font' as const } };

function Label() {
  const { riveFile } = useRiveFile(require('./label.riv'), { referencedAssets: ASSETS });

  return riveFile ? <RiveView file={riveFile} autoPlay style={{ height: 120 }} /> : null;
}

export default function Repro() {
  const [second, setSecond] = useState(false);

  return (
    <View style={{ flex: 1, paddingTop: 80 }}>
      <Label />
      {second ? <Label /> : null}
      <Button title={second ? 'Unmount second' : 'Mount second'} onPress={() => setSecond((on) => !on)} />
    </View>
  );
}
  1. Launch. The first label draws its text.
  2. Tap Mount second. Both labels draw their text.
  3. Tap Unmount second. The first label loses its font. Its text draws nothing from the next frame that shapes it, for example after its text changes through data binding or a state change. Its shapes keep drawing.
  4. Tap Mount second again. Registering the name again re-applies the font to the first label as well.

The steps we ran in our app have the same shape: file A mounted, file B with the same referenced font mounted and then disposed, then A's text set through a view model. A's text did not draw. In a control run without file B, it drew every time.

The snippet above is the same pattern reduced to its parts. We have not run it as a standalone app.

Expected

Disposing a file releases only that file's hold on a referenced asset. Other live files keep drawing with it.

Actual

Disposing any file that registered the name removes the asset from all live files.

Cause

  • iOS, ios/new/AssetLoader.swift:
    • registerAsset calls worker.removeGlobalFontAsset(name), then decodes, then calls addGlobalFontAsset(font, name:).
    • RegisteredAsset.release(on:), which HybridRiveFile.dispose() calls, runs removeGlobalFontAsset(name) unconditionally.
  • Android, android/src/new/.../AssetLoader.kt:
    • registerAsset calls riveWorker.unregisterFont(name) before FontAsset.fromBytes and register(name).
    • RegisteredAsset.release() calls asset.unregister(name) and asset.close().
    • close() deletes the font handle. If that handle is the one currently backing the name, deleting it also unregisters the name.
  • rive-runtime command_server.cpp, shared by both:
    • removeFontFileAsset → removeFont(name) + applyFont(name, nullptr) clears the font in every loaded file asset with that name.
    • deleteFont does the same for every name backed by the deleted handle.
    • addFontFileAsset re-applies the font to all of them, which is why step 4 recovers.

The remove-then-decode-then-add order in registerAsset also clears the name in every other live file for as long as the decode takes, whenever a new file registers it. The same applies to images and audio.

Suggested fix

Count holders per (type, name) in AssetLoader, and remove the name only when the last file holding it is disposed.

Register by replacing in place. addGlobal*Asset and register(name) already replace the name's resource (setResource overwrites it, and the worker swaps its dictionary entry), so the remove before the decode is not needed.

On Android, also keep the asset that currently backs a name open until it is replaced or its name is released, since closing it unregisters the name.

Happy to open a PR with this.

Workaround

Give every .riv its own font asset id: import the font into each file separately instead of copying it from another file. Then no two live files share a name. The same file mounted twice still collides.

Nothing in the public API helps here. updateReferencedAssets is not supported on the new backend.

主要语言
TypeScript
星标
159
派生
17
平均合并
2 天 15 小时
30 天内合并 PR
24

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rive-app/rive-nitro-react-native 的其他 Issue

查看 rive-app/rive-nitro-react-native 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。