Skip to content

Commit

Permalink
Avoid keeping module file open in tools/webassembly.py API (#17271)
Browse files Browse the repository at this point in the history
These was a bug in my implementation of `@cache` that I added in #17255
where it was keeping the underlying wasm file open and never actually
closing it.  This is because I was using the incoming arguments as the
hash key instead of their hash value.

The bug only manifested on windows where open files are locked in some
way prevented further actions from modifying them and resulting is a
strange error:

```llvm-objcopy.exe: error: permission denied```
  • Loading branch information
sbc100 authored Jun 19, 2022
1 parent 854adf6 commit 3ebf9f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ jobs:
core2.test_em_asm_unicode
core2.test_em_js
core2.test_em_js_pthreads
other.test_c_preprocessor
other.test_prejs_unicode
other.test_em_js_side_module
other.test_es5_transpile
Expand Down
33 changes: 16 additions & 17 deletions tools/webassembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from collections import namedtuple
from enum import IntEnum
from functools import wraps
import logging
import os
import sys
Expand Down Expand Up @@ -54,28 +55,26 @@ def read_sleb(iobuf):
return leb128.i.decode_reader(iobuf)[0]


# TODO(sbc): Use the builtin functools.cache once we update to python 3.9
def cache(f):
results = {}
def cache(method):

def helper(*args, **kwargs):
@wraps(method)
def wrapper(self, *args, **kwargs):
assert not kwargs
key = args
if key not in results:
results[key] = f(*args, **kwargs)
return results[key]
key = method
if key not in self._cache:
self._cache[key] = method(self, *args, **kwargs)
return self._cache[key]

return helper
return wrapper


def once(f):
done = False
def once(method):

def helper(*args, **kwargs):
nonlocal done
if not done:
done = True
f(*args, **kwargs)
@wraps(method)
def helper(self, *args, **kwargs):
key = method
if key not in self._cache:
self._cache[key] = method(self, *args, **kwargs)

return helper

Expand Down Expand Up @@ -167,7 +166,7 @@ def __init__(self, filename):
version = self.buf.read(4)
if magic != MAGIC or version != VERSION:
raise InvalidWasmError(f'{filename} is not a valid wasm file')
self._done_calc_indexes = False
self._cache = {}

def __del__(self):
if self.buf:
Expand Down

0 comments on commit 3ebf9f1

Please sign in to comment.