Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion esm/worker/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,22 @@ export default (...args) =>
const { postMessage } = worker;
const isHook = this instanceof Hook;

// basic per-worker semaphore to help loops getting unstuck
let stuck = false;

const sync = assign(
worker.proxy,
{ importJS, importCSS },
{
importJS,
importCSS,
// returns the current status of the semaphore
isStuck: () => stuck,
// when invoked through the worker it resets the semaphore
// to make it unstuck again
notStuck() {
stuck = false;
},
},
);

const resolver = withResolvers();
Expand All @@ -63,6 +76,10 @@ export default (...args) =>
});

defineProperties(worker, {
// when invoked on the main thread it will set stuck
// so that any worker loop checking the status can
// eventually throw an error.
unstuck: { value() { stuck = true } },
sync: { value: sync },
ready: { value: resolver.promise },
postMessage: {
Expand Down
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polyscript",
"version": "0.19.8",
"version": "0.19.9",
"description": "PyScript single core to rule them all",
"main": "./esm/index.js",
"types": "./types/polyscript/esm/index.d.ts",
Expand Down Expand Up @@ -88,13 +88,13 @@
"@webreflection/utils": "^0.1.1",
"basic-devtools": "^0.1.6",
"codedent": "^0.1.2",
"coincident": "^4.0.30",
"coincident": "^4.1.0",
"html-escaper": "^3.0.3",
"reflected-ffi": "^0.6.3",
"reflected-ffi": "^0.7.0",
"sticky-module": "^0.1.1",
"to-json-callback": "^0.1.1"
},
"worker": {
"blob": "sha256-KAjqLKnATwMGpCYHIqvKU0qTsPMg89Hg6gH2nz5FBys="
"blob": "sha256-Epkjn7aMtcD7+5b5zqBH2CMgCLalXtFSWfuOZ7eOVCg="
}
}
28 changes: 28 additions & 0 deletions test/unstuck/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<script type="module" src="../../dist/index.js"></script>
<script id="terminal" type="micropython" worker>
from polyscript import xworker
is_stuck = xworker.sync.isStuck
not_stuck = xworker.sync.notStuck

def unstuck(condition):
if condition and is_stuck():
not_stuck()
raise RuntimeError('Worker is stuck')

return condition

while unstuck(True):
import time
time.sleep(1)
print('stuck in a loop')
</script>
</head>
<body>
<button onclick="terminal.xworker.unstuck()">Unstuck</button>
</body>
</html>