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

Problem getting an app with bcrypt to run #1189

Closed
boxed opened this issue Apr 16, 2023 · 15 comments
Closed

Problem getting an app with bcrypt to run #1189

boxed opened this issue Apr 16, 2023 · 15 comments
Labels
bug A crash or error in behavior. iOS The issue relates to Apple iOS mobile support.

Comments

@boxed
Copy link

boxed commented Apr 16, 2023

Describe the bug

I have a very simple app I'm trying to write that uses paramiko for SSH. It crashes on startup in the simulator with an ImportError.

Steps to reproduce

This is my code:

import paramiko
import toga
from paramiko.client import (
    WarningPolicy,
)
from toga.style import Pack
from toga.style.pack import COLUMN


class RebootShow(toga.App):

    def startup(self):
        """
        Construct and show the Toga application.

        Usually, you would add your application to a main content box.
        We then create a main window (with a name matching the app), and
        show the main window.
        """
        self.reboot_show(1)
        main_box = toga.Box(style=Pack(direction=COLUMN))

        button = toga.Button(
            "Reboot Show",
            on_press=self.reboot_show,
            style=Pack(padding=5)
        )

        main_box.add(button)

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

    def reboot_show(self, widget):
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(WarningPolicy)
        client.connect('******', username='boxed', password='******')
        ssh_stdin, ssh_stdout, ssh_stderr = client.exec_command("""osascript -e 'tell app "System Events" to restart'""")
        print(ssh_stdout.read(), ssh_stderr.read())


def main():
    return RebootShow()

with hostname and password censored.

Expected behavior

No crash

Screenshots

No response

Environment

  • Operating System: macOS
  • Python version: 3.9. Also tried 3.10 before.
  • Software versions:
    • Briefcase: 0.3.14
    • Toga: 0.3.1

Logs

briefcase.2023_04_16-19_52_55.run.log

Additional context

No response

@rmartin16
Copy link
Member

https://github.com/beeware/briefcase/files/11243064/briefcase.2023_04_16-19_52_55.run.log

@boxed, is this the right log file?

This one is documenting a device name issue:

Invalid device device name '00008101-000A21A11A30001E'

@rmartin16 rmartin16 added the iOS The issue relates to Apple iOS mobile support. label Apr 16, 2023
@boxed
Copy link
Author

boxed commented Apr 16, 2023

Ah crap. That's the other issue yea. This is a fresh log file for this issue (I deleted all other logs before :P)

briefcase.2023_04_16-20_53_11.run.log

@rmartin16
Copy link
Member

Thanks. My suspicion is you're not using bcrypt from https://anaconda.org/beeware/bcrypt.

Your log shows the latest version being installed....which would've come from pypi.org:

Using cached bcrypt-4.0.1.tar.gz

When I first tried to run your app in the OP, it failed because I don't have rust installed or anything. (I presume you have the prerequisites installed to build bcrypt.) That was when I noticed the bcrypt version. Once I added bcrypt==3.1.7 to requires in pyproject.toml, the build finished and I could run the app.

Notably, though, this would limit your app to paramiko==2.12 for now.

@freakboy3742
Copy link
Member

Aside from the version problem that @rmartin16 has pointed out, this app will also fail because it's using paramiko.

iOS, Android and Web deployments don't have support for subprocesses because of the sandboxing model they provide. You can only use threads.

Even if it did work... To the best of my knowledge, "osascript" doesn't exist on iPhones, either.

@freakboy3742
Copy link
Member

/me facepalms... paramiko is an SSH client, so this is running osascript on the remote machine. As long as paramiko isn't using subprocesses, it should be fine.

@boxed
Copy link
Author

boxed commented Apr 17, 2023

Oh. Interesting. I don't care about the paramiko version or the bcrypt version (I think!).

I changed to use the specific versions of bcrypt and paramiko as suggested, and I got further I think:

  File "/Users/boxed/Library/Developer/CoreSimulator/Devices/5A1148C9-9857-4B28-9DB6-8DFAC7D270D7/data/Containers/Bundle/Application/2C71ACA0-266C-401C-BCAF-5A55DA74A2E8/RebootShow.app/app_packages/paramiko/pkey.py", line 31, in <module>
    import bcrypt
  File "/Users/boxed/Library/Developer/CoreSimulator/Devices/5A1148C9-9857-4B28-9DB6-8DFAC7D270D7/data/Containers/Bundle/Application/2C71ACA0-266C-401C-BCAF-5A55DA74A2E8/RebootShow.app/app_packages/bcrypt/__init__.py", line 25, in <module>
    from . import _bcrypt
ModuleNotFoundError: No module named '_cffi_backend'
Application has crashed!

I locked cffi to the version on anaconda too as a cargo cult idea, but that didn't change matters.

@freakboy3742
Copy link
Member

Hrm - that's more interesting bug - it suggests the bcrypt module isn't working as expected. I'll need to dig into this some more.

Unfortunately, I'm about to jump on a plane to head to PyCon US, so I probably won't be able to look into this for at least a couple of days. In case I do get a chance - you're using bcrypt==3.1.7, correct?

@boxed
Copy link
Author

boxed commented Apr 17, 2023

Correct

@rmartin16
Copy link
Member

FWIW....the app in the OP runs for me on the iOS simulator for iPhone 14 Pro using Python 3.10. (Although, I modified it to just echo $PATH on a local linux machine.)

I would try to confirm that the package of cffi being used is from Anaconda's package server. I would expect Anaconda's server to take precedence since they are both advertising the same latest version. Although, I suppose it's possible pip is picking up a cached version of pypi.org's package maybe.

If you upload another log file, I'd be happy to take a look.

@boxed
Copy link
Author

boxed commented Apr 17, 2023

@rmartin16
Copy link
Member

briefcase.2023_04_17-20_12_03.run.log

Thanks. Unfortunately, the Briefcase command for that log doesn't include -r so none of the information about the requirements is included.

I was able to confirm paramiko works for me with simulated iPhone 14 plus like you used; although, while yours is using iOS 16.4, my simulator is using iOS 16.1. I'm also running on x86 (albeit virtualized) while you're using ARM.

I'd also try deleting the build directory in your project to ensure the build environment hasn't been tainted by any previous runs.

@boxed
Copy link
Author

boxed commented Apr 17, 2023

huh. I did rm -rf build/ and rebuilt with the -r flag. The app starts.

@rmartin16
Copy link
Member

huh. I did rm -rf build/ and rebuilt with the -r flag. The app starts.

Good to hear; I'd guess this likely happened because the original install of bcrypt would've installed cffi from pypi.org. But then once bcrypt==3.1.7 was added, the pip install --upgrade command perceived that cffi was already installed and therefore, the app was built with the cffi from pypi.org instead of Anaconda.

@boxed
Copy link
Author

boxed commented Apr 17, 2023

Maybe. But I ran with -r -d which shows it downloading the dependency. My spidey sense is going off :P

@mhsmith mhsmith closed this as completed Oct 2, 2023
@mhsmith
Copy link
Member

mhsmith commented Oct 2, 2023

If this happens again with Briefcase 0.3.14 or 15, please comment on this issue. If you're using Briefcase 0.3.16 or later, iOS support has changed significantly, so it would be better to create a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior. iOS The issue relates to Apple iOS mobile support.
Projects
None yet
Development

No branches or pull requests

4 participants