vercel / fun

ƒun - Local serverless function λ development runtime
Apache License 2.0
482 stars 26 forks source link

For `vercel dev`, it said `HTTP Error 500: Internal Server Error` (python3) #56

Closed owtotwo closed 2 years ago

owtotwo commented 4 years ago

There is something wrong with vercel dev command in my Windows 10.

# date.py
from http.server import BaseHTTPRequestHandler
from datetime import datetime

class handler(BaseHTTPRequestHandler):

  def do_GET(self):
    self.send_response(200)
    self.send_header('Content-type', 'text/plain')
    self.end_headers()
    self.wfile.write(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')).encode())
    return

I ran command vercel dev, then it said urllib.error.HTTPError: HTTP Error 500: Internal Server Error when it called req = urllib.request.urlopen(url, data) in line 31 in bootstrap.py.

After I add print(url) in bootstrap.py, line 31. The logs showed:

PS C:\Users\AT\Desktop\test_vercel_dev> vercel dev Vercel CLI 20.1.0 dev (beta) — https://vercel.com/feedback

Ready! Available at http://localhost:3000 Building @vercel/python@latest:api/date.py Installing required dependencies... Built @vercel/python@latest:api/date.py [3s] using HTTP Handler using HTTP Handler http://127.0.0.1:13320/2018-06-01/runtime/invocation/next (<======= print(url) output!!!) <class 'OSError'> http://127.0.0.1:13320/2018-06-01/runtime/invocation/b6544f1d-ec26-40f2-bb76-02f69451c86c/error (<======= print(url) output!!!) http://127.0.0.1:13320/2018-06-01/runtime/invocation/next (<======= print(url) output!!!) http://127.0.0.1:13320/2018-06-01/runtime/invocation/next (<======= print(url) output!!!) TypeError: Object prototype may only be an Object or null: undefined at Function.setPrototypeOf () at new LambdaError (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7870:16)
at Lambda. (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7990:27) at Generator.next () at fulfilled (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7898:58) at processTicksAndRejections (internal/process/task_queues.js:97:5) Traceback (most recent call last): File "C:\Users\AT\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 148, in TypeError: Cannot read property 'resolve' of null at RuntimeServer. (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8526:31)
at Generator.next () at C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8433:71 at new Promise () at module.exports.__webpack_modules.58371.awaiter (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8429:12) at RuntimeServer.handleNextInvocation (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8517:16) at RuntimeServer. (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8488:33)
at Generator.next () at C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8433:71 at new Promise () lambda_runtime_main() File "C:\Users\AT\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 131, in lambda_runtime_main (event, context) = lambda_runtime_next_invocation() File "C:\Users\AT\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 57, in lambda_runtime_next_invocation res = LambdaRequest('invocation/next') File "C:\Users\AT\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 32, in init req = urllib.request.urlopen(url, data) (<======= this line crashed!!!) File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 531, in open response = meth(req, response) File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 640, in http_response
response = self.parent.error( File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 569, in error return self._call_chain(args) File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(
args) File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 500: Internal Server Error


In addition: I did the same thing in WSL(Ubuntu18.04) in this Windows PC, it works well.

So maybe it could not reproduce in other PC.

owtotwo commented 4 years ago

A reproducible public repo: test_vercel_dev Steps:

  1. git clone https://gitlab.com/owtotwo/test_vercel_dev.git
  2. cd test_vercel_dev
  3. vercel dev (I have logined)
  4. Something wrong in file C:\Users\AT\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py by the logs showing.
owtotwo commented 4 years ago

I tried reproducing on my another win10-PC. Its environment is pure. I installed nodejs, python3 from their official website, then installed vercel by npm. Their verisons are as below.

PS C:\Users\at\Desktop> py --version
Python 3.8.5
PS C:\Users\at\Desktop> npm --version
6.14.8
PS C:\Users\at\Desktop> node --version
v14.9.0
PS C:\Users\at\Desktop> vercel --version
Vercel CLI 20.1.0
20.1.0
PS C:\Users\at\Desktop>

Logined by vercel cli, cloned the repo and ran command vercel dev. Then opened the link http://localhost:3000/, it's ok. But if I opened http://localhost:3000/api/date, the logs said as below. (* Different from the first Win10-PC, but the wrong file is the same: C:\Users\at\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py .)

PS C:\Users\at\Desktop\test_vercel_dev> vercel dev
Vercel CLI 20.1.0 dev (beta) — https://vercel.com/feedback
> Ready! Available at http://localhost:3000
> Building @vercel/python@latest:api/date.py
Installing required dependencies...
> Built @vercel/python@latest:api/date.py [4s]
Traceback (most recent call last):
Traceback (most recent call last):
  File "C:\Users\at\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 147, in <module>
  File "C:\Users\at\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 147, in <module>
    lambda_runtime_main()
      File "C:\Users\at\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 127, in lambda_runtime_main
lambda_runtime_main()
  File "C:\Users\at\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 127, in lambda_runtime_main
    fn = lambda_runtime_get_handler()
      File "C:\Users\at\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 113, in lambda_runtime_get_handler
fn = lambda_runtime_get_handler()
      File "C:\Users\at\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 113, in lambda_runtime_get_handler
mod = importlib.import_module(module_name)
      File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
mod = importlib.import_module(module_name)
      File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
      File "__import__(name)
C:\Users\at\AppData\Local\Temp\zeit-fun-8155162f673d2\now__handler__python.py  File "", line C:\Users\at\AppData\Local\Temp\zeit-fun-8155162f673d2\now__handler__python.py200", line 
200
async def receive(self):
         async def receive(self):
                 ^
 SyntaxError : ^
iSyntaxErrornvalid syntax:
invalid syntax
TypeError: Object prototype may only be an Object or null: undefined
    at Function.setPrototypeOf (<anonymous>)
    at new LambdaError (C:\Users\at\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7870:16)     
    at Lambda.<anonymous> (C:\Users\at\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7990:27)  
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\at\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7898:58)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
factoryDestroyError {
  err: Error: Command failed: taskkill /pid 1992 /T /F
  ����: û���ҵ����� "1992"��

      at ChildProcess.exithandler (child_process.js:308:12)
      at ChildProcess.emit (events.js:314:20)
      at ChildProcess.EventEmitter.emit (domain.js:486:12)
      at maybeClose (internal/child_process.js:1047:16)
      at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5) {
    killed: false,
    code: 128,
    signal: null,
    cmd: 'taskkill /pid 1992 /T /F'
  }
}
owtotwo commented 3 years ago

Update to Vercel CLI 20.1.4 dev (beta), do the same things like above, and it reported different errors like below.

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

~\..\test_vercel_dev $ vercel dev
Vercel CLI 20.1.4 dev (beta) — https://vercel.com/feedback
> Ready! Available at http://localhost:3000
> Building @vercel/python@latest:api/date.py
Installing required dependencies...
> Built @vercel/python@latest:api/date.py [6s]
TypeError: Object prototype may only be an Object or null: undefined
    at Function.setPrototypeOf (<anonymous>)
    at new LambdaError (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7870:16)   
    at Lambda.<anonymous> (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7990:27)
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7898:58)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
jtuttas commented 3 years ago

Same problem here ! But works without dev!

Carduelis commented 2 years ago

I can confirm the behavior. Have similar errors <class 'OSError'> using vercel dev, including the latest greatest (at the moment of the comment) version: Vercel CLI 23.1.2.

I've installed the Python 3.8.6 (the same as production runtime of Vercel), and got an error in bootstrap.py file: <root>\co.zeit.fun\Cache\runtimes\python\bootstrap.py.

Which produces a javascript error: TypeError: Cannot read property 'resolve' of null inside of npm\node_modules\vercel\dist\index.js:11569:31 file.

Carduelis commented 2 years ago

UPD. As @owtotwo mentioned, it works on WSL. In my case it was Ubuntu 20.

LeandroAlberti commented 2 years ago

Same problem here

PS C:> py -V Python 3.10.2 PS C:> pip -V pip 22.0.3 PS C:> vercel --version Vercel CLI 24.0.0 24.0.0 PS C:> node --version v14.17.2

stkevintan commented 2 years ago

Same problem here

stkevintan commented 2 years ago

Hi, guys, I finally figure out why it fails in windows: the stderr will emit data event multiple times when spawning the python executable bin, for instance:

child.stderr.on('data', (d) => {
    console.log('stderr data emitted:', d.toString());
});
stderr data emitted: Python was not found; run without arguments
stderr data emitted:  to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

Thus, if the python binary probing failed, the handle function (as the callback of stderr) will be incorrectly triggered multiple times: https://github.com/vercel/fun/blob/af20f0cd2d56c6949257a925bb1113fa48a761b4/src/runtimes/python3/bootstrap.ts#L27

However, handler is only expected to be called once, otherwise, it will spawn multiple processes of bootstrap.py then consequently call the /invocation/next endpoint multiple times: https://github.com/vercel/fun/blob/af20f0cd2d56c6949257a925bb1113fa48a761b4/src/runtimes/python/bootstrap.py#L56

Finally, the runtime-server will receive multiple invokation/next requests, and it will fail at this point: https://github.com/vercel/fun/blob/af20f0cd2d56c6949257a925bb1113fa48a761b4/src/runtime-server.ts#L109-L110

the nextDeferred will be immediately set to null, so after the first call, all the subsequence calls will fail with such error:

TypeError: Cannot read property 'resolve' of null

I will send a PR soon to fix it.

dmarcucci commented 2 years ago

For anybody else that may come across this and still has this problem, here is a solution I found that fixed the issue for me.