Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Memory leak when invoking functionality from a WASM plugin #6404

Closed
stahlbauer opened this issue Nov 11, 2022 · 11 comments · Fixed by #6853
Closed

[Bug]: Memory leak when invoking functionality from a WASM plugin #6404

stahlbauer opened this issue Nov 11, 2022 · 11 comments · Fixed by #6853
Assignees
Labels
Milestone

Comments

@stahlbauer
Copy link
Contributor

Describe the bug

I have written a plugin that uses the metadata.source_map to guide some decisions. Calling methods of this object, in particular span_to_snippet, leads to a memory leak.

Here is an example of a plugin that reproduces the issue:

#[plugin_transform]
pub fn process_transform(
    mut program: Program,
    metadata: TransformPluginProgramMetadata,
) -> Program {

    for i in 1..50000 {        
        let j: u32 = i;
        println!("i {} j {}", i, j);
        metadata.source_map.span_to_snippet(Span::new(BytePos(j), BytePos(j+10), SyntaxContext::empty()));
    }

    program
}

I have used this plugin in my test environment which invokes it two times. This results in the following memory graph:

with_sourcemap_access_leak

Removing the loop also eliminates the memory leak.

without_sourcemap_access

Note that I am using a version of SWC that allows for using the full 4GB of WASM memory---see #6378 .

Input code

No response

Config

No response

Playground link

No response

Expected behavior

The memory should be freed after use.

Actual behavior

No response

Version

1.3.14

Additional context

No response

@kwonoj
Copy link
Member

kwonoj commented Nov 12, 2022

I'm bit unsure if this is expected amount of pressure or if there's an actual leak. If leak is true, it'll be probably allocated buffer in the wasm memory space is not explicitly freed somewhere.

@stahlbauer
Copy link
Contributor Author

@kwonoj Are you planing to address this proactively? Which means are currently in place to free memory allocated in context of sharing data between the native process and the WASM world?

@kwonoj
Copy link
Member

kwonoj commented Nov 17, 2022

Need confirmation on #6404 (comment) .

@stahlbauer
Copy link
Contributor Author

Do you need this confirmation from me? In which form? I would have to get into WASM memory management first to understand the foundations.

Generally, I would not call the observable behavior a desired behavior :-) What I learned is that WASM does not do garbage collection. So where should the "pressure" come from?

@kwonoj
Copy link
Member

kwonoj commented Nov 17, 2022

I meant someone. If I have some time I could, but I can't promise anything.

I am aware wasm / rust both are not GC collectable. But there still can be pressure if some memories are allocated in very short time frame, which is example code does exactly. The exact thing need to be confirmed is an evidence allocated memory is not dropped correctly once it is completed used and goes out of the scope from plugin transform.

@stahlbauer
Copy link
Contributor Author

stahlbauer commented Nov 17, 2022

Thanks for the clarification. Just wanted to ensure that you are not waiting for exactly my confirmation (which could delay fixing this significant issue without intent).

@kdy1 kdy1 self-assigned this Jan 21, 2023
@kdy1
Copy link
Member

kdy1 commented Jan 25, 2023

I investigated this, and it seems like this happens only if the value sent from the host to the plugin is Err

(To be exact, it seems like it requires to be out of range)

@kdy1
Copy link
Member

kdy1 commented Jan 25, 2023

My mistake, I can't reproduce this with --nocapture
Maybe a large file required?

@kdy1
Copy link
Member

kdy1 commented Jan 25, 2023

image

image

With a large file, I can reproduce with Ok, and the plugin halts after 4.21GB

@swc-bot
Copy link
Collaborator

swc-bot commented Feb 25, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Feb 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

4 participants