Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdin is passed incorrectly #14

Closed
maiermic opened this issue Mar 1, 2024 · 5 comments
Closed

stdin is passed incorrectly #14

maiermic opened this issue Mar 1, 2024 · 5 comments

Comments

@maiermic
Copy link

maiermic commented Mar 1, 2024

Steps to Reproduce (for bugs)

Pull python3-debian template from store. Replace index.py with

from pathlib import Path
from zipfile import is_zipfile

if __name__ == "__main__":
    print(is_zipfile(Path('/dev/stdin')))

Create a ZIP file:

echo "Hello World" > hello.txt
zip hello.zip hello.txt

A direct call without Watchdog should look like this

$ python index.py < hello.zip
True

Now deploy a function is-zip with the template (uses fwatchdog to call python3 index.py).

Expected Behaviour

$ faas-cli invoke is-zip < hello.zip
True

Current Behaviour

$ faas-cli invoke is-zip < hello.zip
False

Context

Related to openfaas/templates#205

Your Environment

  • Docker version: 25.0.3

  • Kubernetes

  • Operating System and version: MacOS 14.3.1 (23D60)

$ faas-cli version
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
      |_|

CLI:
 commit:  a3e72b5881c4efcc7a366a2e8dc384399c807dfc
 version: 0.16.23

Gateway
 uri:     http://127.0.0.1:8080
 version: 0.27.5
 sha:     5c13f1f01c6a9f596857d81c889d77bd04fbc5c1


Provider
 name:          faas-netes-ce
 orchestration: kubernetes
 version:       0.18.1
 sha:           e35f8441df8355654fcb56eab55d4575aabcdcfd
@alexellis
Copy link
Member

Hi, thanks for your interest.

I'm not clear what you're trying to do here, or what you're trying to say, but this template has been used extensively over years.

You'll need to post your function's source code so we can see if this is something we want to support or not.

Alex

@maiermic
Copy link
Author

maiermic commented Mar 6, 2024

I'm not clear [...] what you're trying to say, but this template has been used extensively over years

The Python script receives different bytes via stdin if called by watchdog. It's not about the template. I just used the template python3-debian as a base to show case this issue with minimal code or steps to reproduce. You can use any function's source code you want to deploy is-zip. It isn't called by the modified template anyway.

The change of the input might be caused by faas-cli, watchdog or anything in between. The issue does not occur with the Python Flask Templates, which use of-watchdog instead of classic-watchdog. Hence, I assume this issue is caused by classic-watchdog.

I assume, this issue exists with every template that uses classic-watchdog, but I have only tested it with Python, yet. It needs more investigation.

@maiermic
Copy link
Author

maiermic commented Mar 8, 2024

I checked something similar today. I deployed the alpine function from store, which echos the HTTP input using cut. Then I ran:

faas-cli invoke alpine --gateway https://faasd.dieschulapp.de < hello.zip > hello.zip.copy
cmp hello.zip hello.zip.copy
diff hello.zip hello.zip.copy

According to cmp and diff, input and output are the same. It could be that I have jumped to premature conclusions:

The Python script receives different bytes via stdin if called by watchdog.

I changed index.py to

from pathlib import Path

if __name__ == "__main__":
    print(Path('/dev/stdin').read_bytes())

and get the same results for

python index.py < hello.zip

and

faas-cli invoke is-zip < hello.zip

This issue might be related to Python, the (Python) environment, zipfile or is_zipfile in specific. It needs further investigation, but I'm convinced that is not an issue related to classic-watchdog.

@maiermic maiermic closed this as completed Mar 8, 2024
@maiermic
Copy link
Author

maiermic commented Mar 8, 2024

I found out that

$ python index.py < hello.zip
True
$ cat hello.zip | python index.py
False

is_zipfile somehow has to read bytes from the stream. I assume it does try to rewind (in between and afterwards), but that does only work with something like a regular file stream. It works with

from pathlib import Path
from tempfile import NamedTemporaryFile
from zipfile import is_zipfile

if __name__ == "__main__":
  with NamedTemporaryFile() as temp_file:
        temp_file.write(stdin.read_bytes())
        temp_file.flush()
        file = Path(temp_file.name)
        print(is_zipfile(file))

@alexellis
Copy link
Member

No problem.

Can I ask what your use-case is for OpenFaaS? Are you a user already or considering using it for a new project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants