Skip to content

Commit ab2e4b5

Browse files
committed
Added XWorker unstuck feature
1 parent 205e693 commit ab2e4b5

File tree

6 files changed

+60
-15
lines changed

6 files changed

+60
-15
lines changed

docs/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/worker/class.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,22 @@ export default (...args) =>
4444
const { postMessage } = worker;
4545
const isHook = this instanceof Hook;
4646

47+
// basic per-worker semaphore to help loops getting unstuck
48+
let stuck = false;
49+
4750
const sync = assign(
4851
worker.proxy,
49-
{ importJS, importCSS },
52+
{
53+
importJS,
54+
importCSS,
55+
// returns the current status of the semaphore
56+
isStuck: () => stuck,
57+
// when invoked through the worker it resets the semaphore
58+
// to make it unstuck again
59+
notStuck() {
60+
stuck = false;
61+
},
62+
},
5063
);
5164

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

6578
defineProperties(worker, {
79+
// when invoked on the main thread it will set stuck
80+
// so that any worker loop checking the status can
81+
// eventually throw an error.
82+
unstuck: { value() { stuck = true } },
6683
sync: { value: sync },
6784
ready: { value: resolver.promise },
6885
postMessage: {

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@
8888
"@webreflection/utils": "^0.1.1",
8989
"basic-devtools": "^0.1.6",
9090
"codedent": "^0.1.2",
91-
"coincident": "^4.0.30",
91+
"coincident": "^4.1.0",
9292
"html-escaper": "^3.0.3",
93-
"reflected-ffi": "^0.6.3",
93+
"reflected-ffi": "^0.7.0",
9494
"sticky-module": "^0.1.1",
9595
"to-json-callback": "^0.1.1"
9696
},
9797
"worker": {
98-
"blob": "sha256-KAjqLKnATwMGpCYHIqvKU0qTsPMg89Hg6gH2nz5FBys="
98+
"blob": "sha256-Epkjn7aMtcD7+5b5zqBH2CMgCLalXtFSWfuOZ7eOVCg="
9999
}
100100
}

test/unstuck/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
6+
<script type="module" src="../../dist/index.js"></script>
7+
<script id="terminal" type="micropython" worker>
8+
from polyscript import xworker
9+
is_stuck = xworker.sync.isStuck
10+
not_stuck = xworker.sync.notStuck
11+
12+
def unstuck(condition):
13+
if condition and is_stuck():
14+
not_stuck()
15+
raise RuntimeError('Worker is stuck')
16+
17+
return condition
18+
19+
while unstuck(True):
20+
import time
21+
time.sleep(1)
22+
print('stuck in a loop')
23+
</script>
24+
</head>
25+
<body>
26+
<button onclick="terminal.xworker.unstuck()">Unstuck</button>
27+
</body>
28+
</html>

0 commit comments

Comments
 (0)