dfinity / prettier-plugin-motoko

A code formatter for the Motoko smart contract language.
Apache License 2.0
28 stars 1 forks source link

Wasm memory leak while formatting? #141

Open ByronBecker opened 3 hours ago

ByronBecker commented 3 hours ago

Just recently ran into this weird formatting memory issue. Seems to run out of memory just when this one file is included.

% npx prettier --write --plugin=prettier-plugin-motoko canisters/lib/IC/Management/lib.mo
canisters/lib/IC/Management/lib.mopanicked at /rust/deps/dlmalloc-0.2.6/src/dlmalloc.rs:1198:13:
assertion failed: psize <= size + max_overhead

Stack:

Error
    at module.exports.__wbg_new_693216e109162396 (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/wasm/pkg/nodejs/wasm.js:200:17)
    at wasm://wasm/00061082:wasm-function[166]:0x15f78
    at wasm://wasm/00061082:wasm-function[59]:0x11931
    at wasm://wasm/00061082:wasm-function[102]:0x14757
    at wasm://wasm/00061082:wasm-function[99]:0x145b5
    at wasm://wasm/00061082:wasm-function[143]:0x1574c
    at wasm://wasm/00061082:wasm-function[70]:0x12fba
    at module.exports.find_comments (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/wasm/pkg/nodejs/wasm.js:129:14)
    at preprocess (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/lib/parsers/motoko-tt-parse/preprocess.js:54:45)
    at Object.parse (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/lib/parsers/motoko-tt-parse/parse.js:13:37)

[error] canisters/lib/IC/Management/lib.mo: RuntimeError: unreachable
[error]     at wasm://wasm/00061082:wasm-function[59]:0x11957
[error]     at wasm://wasm/00061082:wasm-function[102]:0x14757
[error]     at wasm://wasm/00061082:wasm-function[99]:0x145b5
[error]     at wasm://wasm/00061082:wasm-function[143]:0x1574c
[error]     at wasm://wasm/00061082:wasm-function[70]:0x12fba
[error]     at module.exports.find_comments (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/wasm/pkg/nodejs/wasm.js:129:14)
[error]     at preprocess (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/lib/parsers/motoko-tt-parse/preprocess.js:54:45)
[error]     at Object.parse (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/lib/parsers/motoko-tt-parse/parse.js:13:37)
[error]     at parse4 (file:///Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier/index.mjs:20685:24)
[error]     at async coreFormat (file:///Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier/index.mjs:21146:7)
byronbecker@MacBook-Pro topmeup % export WASM_MEM_MAX=4096
byronbecker@MacBook-Pro topmeup % npx prettier --write --plugin=prettier-plugin-motoko canisters/lib/IC/Management/lib.mo
canisters/lib/IC/Management/lib.mopanicked at /rust/deps/dlmalloc-0.2.6/src/dlmalloc.rs:1198:13:
assertion failed: psize <= size + max_overhead

Stack:

Error
    at module.exports.__wbg_new_693216e109162396 (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/wasm/pkg/nodejs/wasm.js:200:17)
    at wasm://wasm/00061082:wasm-function[166]:0x15f78
    at wasm://wasm/00061082:wasm-function[59]:0x11931
    at wasm://wasm/00061082:wasm-function[102]:0x14757
    at wasm://wasm/00061082:wasm-function[99]:0x145b5
    at wasm://wasm/00061082:wasm-function[143]:0x1574c
    at wasm://wasm/00061082:wasm-function[70]:0x12fba
    at module.exports.find_comments (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/wasm/pkg/nodejs/wasm.js:129:14)
    at preprocess (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/lib/parsers/motoko-tt-parse/preprocess.js:54:45)
    at Object.parse (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/lib/parsers/motoko-tt-parse/parse.js:13:37)

[error] canisters/lib/IC/Management/lib.mo: RuntimeError: unreachable
[error]     at wasm://wasm/00061082:wasm-function[59]:0x11957
[error]     at wasm://wasm/00061082:wasm-function[102]:0x14757
[error]     at wasm://wasm/00061082:wasm-function[99]:0x145b5
[error]     at wasm://wasm/00061082:wasm-function[143]:0x1574c
[error]     at wasm://wasm/00061082:wasm-function[70]:0x12fba
[error]     at module.exports.find_comments (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/wasm/pkg/nodejs/wasm.js:129:14)
[error]     at preprocess (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/lib/parsers/motoko-tt-parse/preprocess.js:54:45)
[error]     at Object.parse (/Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier-plugin-motoko/lib/parsers/motoko-tt-parse/parse.js:13:37)
[error]     at parse4 (file:///Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier/index.mjs:20685:24)
[error]     at async coreFormat (file:///Users/byronbecker/Workspace/Dfinity_Projects/topmeup/node_modules/prettier/index.mjs:21146:7)

Here's the file that it's choking on:

import Prim "mo:⛔";
import Buffer "mo:base/Buffer";
import Result "mo:base/Result";
import Error "mo:base/Error";
import InterfaceSpec "Interface";
import { MAX_BATCH_SIZE } "../../Constants";

module {
  public type TopupTarget = {
    canisterId : Principal;
    cyclesToTopupWith : Nat;
  };

  public func batchDepositCycles(targets : [TopupTarget]) : async* [Result.Result<(), Text>] {
    let depositFutures = Buffer.Buffer<async ()>(0);
    let depositResults = Buffer.Buffer<Result.Result<(), Text>>(targets.size());

    for ({ canisterId; cyclesToTopupWith} in targets.vals()) {
      Prim.cyclesAdd<system>(cyclesToTopupWith);
      depositFutures.add(InterfaceSpec.IC.deposit_cycles({ canister_id = canisterId }));

      if (depositFutures.size() == MAX_BATCH_SIZE) {
        for (future in depositFutures.vals()) {
          try { depositResults.add(#ok(await future)) }
          catch(err) { depositResults.add(#err(Error.message(err))) };
        };
        depositFutures.clear();
      };
    };

    for (future in depositFutures.vals()) {
      try { depositResults.add(#ok(await future)) }
      catch(err) { depositResults.add(#err(Error.message(err))) };
    };

    Buffer.toArray(depositResults);
  };
}
rvanasa commented 3 hours ago

This is a known issue which is most likely caused by the emoji in import Prim "mo:⛔";. You might be able to work around this by replacing the first line with import Prim "mo:prim";.

I looked into this a while ago, and it appears to be some deep-seated issue with how wasm-bindgen translates between JS and Rust strings. I'll increase the priority of fixing this, especially if you continue to run into this after removing the prim emoji.