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

Runtime Error when trying to run native=True #1841

Open
swrpug opened this issue Oct 19, 2023 · 14 comments
Open

Runtime Error when trying to run native=True #1841

swrpug opened this issue Oct 19, 2023 · 14 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@swrpug
Copy link

swrpug commented Oct 19, 2023

Description

I'm getting a Fork vs Spawn error when attempting to run a native app on Ubuntu.

Sample Application (from documentation)

from nicegui import app, ui

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

Invoked from command line from within a virtual environment: python main.py

Trackback

Traceback (most recent call last):
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/main.py", line 15, in <module>
    main()
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/main.py", line 11, in main
    ui.run(native=True, window_size=(400, 300), fullscreen=False)
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/env/lib/python3.11/site-packages/nicegui/run.py", line 180, in run
    ChangeReload(config, target=globals.server.run, sockets=[sock]).run()
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/env/lib/python3.11/site-packages/uvicorn/supervisors/basereload.py", line 49, in run
    self.startup()
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/env/lib/python3.11/site-packages/uvicorn/supervisors/basereload.py", line 85, in startup
    self.process.start()
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
                  ^^^^^^^^^^^^^^^^^
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/context.py", line 288, in _Popen
    return Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/synchronize.py", line 107, in __getstate__
    raise RuntimeError('A SemLock created in a fork context is being '
RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context. This is not supported. Please use the same context to create multiprocessing objects and Process.

OS: Ubuntu 20.04
Python: 3.11.6
Pyenv: 2.3.29-5-g62e2e9c7
nicegui: 1.3.17
pywebview: 4.3.3

@rodja
Copy link
Member

rodja commented Oct 20, 2023

Strange. On Mac it's working fine. @JensOgorek could you check if the error is reproducible on your system?

@JensOgorek
Copy link
Contributor

JensOgorek commented Oct 27, 2023

@rodja I just saw this. Can confirm an error. It starts normally, but clicking on enlarge causes connection lost at nicegui version 1.3.17 and on 1.4.1.

@rodja rodja added the bug Something isn't working label Oct 28, 2023
@muratbedir

This comment was marked as off-topic.

@rodja

This comment was marked as off-topic.

@fdawnxl
Copy link

fdawnxl commented Nov 5, 2023

I also encountered the same situation.Environment related information is as follows:

  • OS Version:ArchLinux Linux 6.5.9-arch2-1
  • Python Version: 3.11.5
  • NiceGUI Version:1.4.1

When I execute the following code in the venv virtual environment, python will report an error:

from nicegui import app, ui

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)
Traceback(part):
  File "/usr/lib/python3.11/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "/usr/lib/python3.11/multiprocessing/synchronize.py", line 107, in __getstate__
    raise RuntimeError('A SemLock created in a fork context is being '
RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context. This is not supported. Please use the same context to create multiprocessing objects and Process.

Because pip cannot be used to directly install python packages under ArchLinux (it will conflict with pacman), and there is no packaged python-nicegui package, so I have been developing in a virtual environment. I used to think that the error was due to executing NiceGUI in a virtual environment, but after reading this article, I tend to think that it is due to incorrect configuration of multiprocessing.(However, I still recommend that you directly use the system python environment to execute the NiceGUI code in a Linux distribution that can use pip to determine whether this problem can be reproduced.)

@dev-robasi
Copy link

It works in windows via a virtual env. Had same error with wsl-ubuntu in a virtual env.

@nimmadev
Copy link

same issue on
system: Linux 6.5.13-7-MANJARO
python: 3.12.1
python installed with pyenv
using virtual env

@rodja rodja added the help wanted Extra attention is needed label Jan 16, 2024
@ahdeddd
Copy link

ahdeddd commented Jan 18, 2024

Same issue here on Manjaro Linux. I installed pywebview with --break-system-packages

@yuanxion
Copy link

yuanxion commented Jan 23, 2024

1."RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context." issue is not happened when running the following sample on my host with Python 3.10 and nicegui (both 1.3.17 & 1.4.12).

// On host
Operating System: Ubuntu 22.04.3 LTS
Kernel: Linux 6.5.0-14-generic
Python: 3.10.13
nicegui: 1.3.17/1.4.12
pywebview: 4.4.1

from nicegui import app, ui

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

2.But I will have another issue when running nicegui with "native=True": Connection terminated unexpectedly, as described #2419.

@electronstudio
Copy link

Add to the top of your code:

import multiprocessing
multiprocessing.set_start_method("spawn", force=True)

On Linux I also had to pip install qtpy pyqt5 PyQtWebEngine

@fdawnxl
Copy link

fdawnxl commented Feb 14, 2024

I've tried the method above, but NicuGUI still throws the same error as before. I think this might be a venv issue. So I created an ubuntu virtual machine (with Gnome desktop) and installed NiceGUI and pywebview in the system python environment. After I installed the relevant dependencies of pywebview, I directly executed the sample code on the homepage. Surprisingly, the program was executed successfully:

Screenshot from 2024-02-15 00-01-24

Since I did not use the same system and kernel as the host machine(Arch Linux, linux-zen) when I created the virtual machine(Ubuntu, linux), I cannot find the problem by controlling variables for the time being. However, this may indicate that there is no problem with the code of NiceGUI itself.
By the way, when using pywebview, you may get an error due to missing dependencies. You can refer to this URL to solve this problem.

@initiateit
Copy link

initiateit commented Mar 6, 2024

Using:

from nicegui import app, ui
import multiprocessing
multiprocessing.set_start_method("spawn", force=True)

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

Getting Error:

RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context. This is not supported. Please use the same context to create multiprocessing objects and Process.

Operating system is: Ubuntu 23.10

Installed via:
python3 -m pip install pywebview nicegui qtpy pyqt5 PyQtWebEngine

@rodja sorry I somehow forgot to copy that into my post.

@Aikufurr
Copy link

Getting same issue

Host:
Operating System: EndeavourOS x86_64
Kernel: 6.8.7-zen1-1-zen
Python: 3.11.8
nicegui: 1.4.15
pywebview: 5.1

@gsal
Copy link

gsal commented May 23, 2024

Getting same issue:

Rocky 9.3
Python 3.11.9
Nicegui 1.4.25
pywebview 5.1

BUT, I cannot install what I need into the system, so, I am working out of a conda environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests