Skip to content

Wasm filter leaks memory #11072

@JasonCrease

Description

@JasonCrease

Bug Report

Describe the bug
The Wasm filter plugin leaks memory. Specifically, every 'tag' and 'record' string. This inevitably leads to running out of Wasm heap space and the filter failing to execute. Typical logging:

[00:00:56:141 - 7B4C51BFF640]: warning: allocate 12 bytes memory failed
[00:00:56:141 - 7B4C51BFF640]: warning: allocate 20 bytes memory failed

This effectively makes the Wasm filter plugin unusable.

To Reproduce
Any Wasm filter produces this. For instance, the examples\filter_rust example code does.

At a high event rate, within a second or two the default 8192 bytes heap is exhausted, and messages appear like this.

[2025/10/26 16:36:49.499396335] [error] Got exception running wasm code: Exception: unreachable
[01:56:28:440 - 7634DEFFF640]: warning: allocate 9 bytes memory failed
[01:56:28:440 - 7634DEFFF640]: warning: allocate 20 bytes memory failed

In fact, you don't even need to have it find your filter. The simplest reproduction case is perhaps this:

pipeline:
  inputs:
    - name: dummy
      tag: my_dummy
      samples: 100000
      rate: 50

  filters:
    - name: wasm
      match: my_dummy
      wasm_path: any_valid_wasm_file.wasm
      function_name: filter_that_does_not_exist
      accessible_paths: .

This will fail in around 5 seconds.

Cause

I believe the cause is the allocation of memory in flb_wasm_call_function_format_json in flb_wasm.c:

fw->tag_buffer = wasm_runtime_module_dup_data(fw->module_inst, tag_data, tag_len+1);
fw->record_buffer = wasm_runtime_module_dup_data(fw->module_inst, record_data, record_len+1);

These two pointers are never freed. The same issue is in flb_wasm_call_function_format_msgpack.

The math works out too. e.g. With the default 8192 byte heap, if the record and tag sum to 200 bytes, you'd expect it to be exhausted in 40 records. For me, it's 38. Doubling the heap delays it to 76 records.

Fix

Adding these two lines seems to fix it:

wasm_runtime_module_free(fw->module_inst, fw->tag_buffer);
wasm_runtime_module_free(fw->module_inst, fw->record_buffer);

Once added and recompiled, I found I could process millions of messages successfully.

Admittedly, I am a newbie in Fluent-Bit and Wasm. I may have simply misunderstood something basic about the memory model. I'd be grateful for any guidance here. However, since the error occurs even if the filter cannot be found, this strongly suggests that this is a genuine issue.

Environment

  • Version used: 4.1.1 Docker image
  • Operating System and version: MacOS

Also happens with

  • Version used: master on Sunday 26 October 2025 (commit id 6345fd1)
  • Configuration: Local build, run in IntelliJ. Run with \./fluent-bit -c fluent-bit.yaml
  • WSL Ubuntu under Windows 11.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions