sagemathinc / cowasm

CoWasm: Collaborative WebAssembly for Servers and Browsers. Built using Zig. Supports Python with extension modules, including numpy.
https://cowasm.org
BSD 3-Clause "New" or "Revised" License
500 stars 26 forks source link

in npx dash-wasm: after the cat command, subsequent commands have problems with stdout #49

Open martin12333 opened 1 year ago

martin12333 commented 1 year ago

(a probably related issue: https://github.com/sagemathinc/cowasm/issues/42 )

// error when I used dash-wasm that was installed by npm


cd ~/10-cowasm/node_modules/dash-wasm

npx dash-wasm -x

(cowasm)$ cat wrongname1
+ cat wrongname1
cat: wrongname1: No such file or directory

(cowasm)$ cat wrongname1
+ cat wrongname1
cat: wrongname1: No such file or directory
cat: stdout: Bad file descriptor

(cowasm)$ cat /cowasm/usr/share/termcap
+ cat /cowasm/usr/share/termcap
cat: stdout: Bad file descriptor

(cowasm)$ echo abc
+ echo abc
sh: 4: echo: echo: I/O error

(cowasm)$ python
+ python
Python 3.11.0 (main, Nov 29 2022, 20:26:05) [Clang 15.0.3 (git@github.com:ziglang/zig-bootstrap.git 0ce789d0f7a4d89fdc4d9571 on wasi
Type "help", "copyright", "credits" or "license" for more information.

# i cannot see what i am typing
# 78*89
# aaaaaaaaa
# quit()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 8] Bad file descriptor

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'aaaaaaaaa' is not defined

Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
OSError: [Errno 8] Bad file descriptor

(cowasm)$

(in browser, commands work correctly)

(but with 1 extra error message)

EDIT I have cut this chapter into a comment

(in a built dash, and using only the cat from the tar package, no errors)

EDIT I have cut this chapter into a comment

LATER: bin/cat from fs.zip ... errors even when I bypassed dash-wasm ... when I ran it from npx kernel dash

in a comment

LATER: the built ./cat from the tar package ... works ok even when I ran it from npx dash-wasm

in a comment

martin12333 commented 1 year ago

(in browser, commands work correctly)

(but with 1 extra error message)


(cowasm)$ cat wrongname1
cat: wrongname1: No such file or directory

(cowasm)$ cat wrongname1
cat: wrongname1: No such file or directory

cat: stdout: Bad file descriptor

(cowasm)$ echo skfjdsf
skfjdsf

(cowasm)$ echo $PATH
/usr/bin:.

(cowasm)$ python
Python 3.11.0 (main, Nov 29 2022, 20:26:05) [Clang 15.0.3 (git@github.com:ziglang/zig-bootstrap.git 0ce789d0f7a4d89fdc4d9571 on wasi
Type "help", "copyright", "credits" or "license" for more information.

>>> fhgfh
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fhgfh' is not defined
martin12333 commented 1 year ago

(in a built dash, and using only the cat from the tar package, no errors)

I have built only dash and the tar package (contains also cpio and cat).

EDIT I don't mean that there were something wrong with the npm version. ... I am still not sure how I should run a built dash correctly. I am still studying.


...
git clone ...
cd ...
make
...
cd ~/cowasm/
./bin/dash-wasm-raw  -ex
# maybe a better invocation is something like
npx kernel dash

# note: but /cowasm does not exist

cd packages
cd tar
cd dist
cd wasm
cd bin
ls -l

./cat wrongname1
...
williamstein commented 1 year ago

The problem might just be that I haven't finished implementing all error handling paths in dash.

The original dash uses setjmp/longjmp for error handling. However, those don't exist in WASM (except via the not-pretty asyncify), so I'm basically rewriting dash to not use them by instead passing back error codes. I did this in some cases, but not everywhere yet. Fortunately, the source code of dash is relatively easy to understand :-). If you know C, and want to help with this, I can give you some pointers.

(Cowasm is BSD licensed, so your contributions are to a fully open source project. I'm having to take a break for a bit from very active cowasm development to catch up on a bunch of other things.)

martin12333 commented 1 year ago

Ok, I will just now add the remaining 2 mentioned experiments ... for completeness, and then I will read your comment again.

martin12333 commented 1 year ago

LATER: bin/cat from fs.zip ... errors even when I bypassed dash-wasm ... when I ran it from npx kernel dash

( https://github.com/sagemathinc/cowasm/blob/main/packages/coreutils/src/utils/cat/cat.c#L98 )

// 2023-01 BTW, cat.c contains

  (fclose(stdout))
cd ~/10-cowasm/node_modules/dash-wasm
npx kernel ./node_modules/@cowasm/dash/dist/wasm/bin/dash -x
ls /cowasm
# /usr/bin/ls: cannot access '/cowasm': No such file or directory
cd dist

zipinfo fs.zip
zipinfo fs.zip |grep cat
unzip fs.zip bin/cat

bin/cat skdjfslkfj
bin/cat 1
cat 1
echo sdfjskf
bin/cat ~/cowasm/R*

(cowasm)$ bin/cat skdjfslkfj
+ bin/cat skdjfslkfj
cat: skdjfslkfj: No such file or directory
(cowasm)$ bin/cat skdjfslkfj
+ bin/cat skdjfslkfj
cat: skdjfslkfj: No such file or directory
(cowasm)$ bin/cat 1
+ bin/cat 1
cat: stdout: Bad file descriptor
(cowasm)$ cat 1
+ cat 1
/usr/bin/cat: write error: Bad file descriptor
(cowasm)$

(cowasm)$ echo sdfjskf
+ echo sdfjskf
dash: 7: echo: echo: I/O error
(cowasm)$
martin12333 commented 1 year ago

LATER: the built ./cat from the tar package ... works ok even when I ran it from npx dash-wasm

(https://github.com/libarchive/libarchive/blob/master/cat/bsdcat.c)

// BTW, I didn't find there anything like fclose(stdout)


cd ~/10-cowasm/node_modules/dash-wasm
npx
npx dash-wasm   -x

cd ~/cowasm

cd packages
cd tar
cd dist
cd wasm
cd bin
ls -l

./cat wrongname1

##./cat cat #  BINARY
##./cat cat
####./cat cat  # problems with my terminal ... so i   restarted ... ... eventually the entire WSL

./cat ~/cowasm/R*
./cat ~/cowasm/R*
# OK
martin12333 commented 1 year ago

I suspected, that after the end of the cat (wasm), the stdout of the parent shell (the dash in wasm) maybe is in the closed(?) state.

Experiment:


cd ~/10-cowasm/node_modules/dash-wasm
npx dash-wasm   -x

python

import os, sys
os.fstat(1)
quit()

os.stat_result(st_mode=8640, st_ino=3, st_dev=98, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1673941490, st_mtime=1673941488, st_ctime=1673880218)


cat anyname1
cat anyname1
echo abc

python

import os, sys
x=os.fstat(1)
print(x, file=sys.stderr)

os.stat_result(st_mode=8640, st_ino=0, st_dev=0, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1673941624, st_mtime=1673941624, st_ctime=1673941624)

I plan to learn how to use the DEBUG env var ... https://www.npmjs.com/package/debug