flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
10.96k stars 423 forks source link

Flet Android, Error SocketException: Failed to create server socket (OS Error: File exists with given unix domain address), address = stdout.sock, port = 0 when saving a file. #2423

Closed User777ll closed 8 months ago

User777ll commented 8 months ago

Description Screenshotwdp

I managed to launch my application, but for some reason when I open music in my application from Explorer it throws me out. the 3rd time I logged in and saw this: SocketException: Failed to create server socket (OS Error: File exists with given unix domain address), address = stdout.sock, port = 0

Code example to reproduce the issue:

Please note that this code works on the computer.

def music_result(e: FilePickerResultEvent):
        if music_picker.result != None and music_picker.result.files != None:
            global tracks__
            tracks__ = []
            global help_tracks__
            help_tracks__ = []
            for my_file_music in music_picker.result.files:
                print(f"Путь: {my_file_music.path}")
                print(f"Название: {my_file_music.name}")
                extract_album_art(my_file_music.path)

                global audio_file_path
#
                audio_file_path = my_file_music.path
                global music_name
                music_name = my_file_music.name

                global track
                track = {"title": music_name, "audio_file_path": audio_file_path}
                tracks__.append(track)
#
    music_picker = FilePicker(on_result=music_result)

Describe the results you received:

Describe the results you expected: I expected everything to work. Additional information you deem important (e.g. issue happens only occasionally):

It is important that this problem only works in .apk When I run the program in vscode everything works.

the problem happens when I upload a file and click save, the file should be saved in page.client_storage and the page will refresh but when I click on the "Save file" button it throws me out, I can provide a video.

Flet version (pip show flet):

Name: flet
Version: 0.19.0
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page:
Author: Appveyor Systems Inc.
Author-email: hello@flet.dev
License: Apache-2.0

Operating system:

Error on Android, but my version on computer: windows 10

Additional environment details: my imports in the application (from flet import from math import from time import sleep

import os import eyed3 import piglet)

FeodorFitsner commented 8 months ago

Can you provide a full and working app to reproduce the issue please?

MaxXilon commented 8 months ago

My app also returned this error Here's the code:-

import flet as ft
import requests
import os
import atexit

socket_path = 'stdout.sock'

def get_remote_content():
    # The URL with the actual endpoint 
    response = requests.get("[my_URL]/assets/update/content.json")
    content = response.json()
    return content.get("welcome_text", "Hello")

def cleanup_socket():
    """
    Cleanup the Unix domain socket file if it exists.
    """
    if os.path.exists(socket_path):
        os.remove(socket_path)

def main(page: ft.Page):
    # Register the cleanup function to run on app exit
    atexit.register(cleanup_socket)

    # Check if the socket file already exists and remove it
    cleanup_socket()

    # Create a Text widget with a default text
    welcome_text = ft.Text(value="Hello", size=20)

    # Function to update Text widget content
    def update_content():
        # Fetch the updated content
        new_content = get_remote_content()
        # Update the Text widget with the new content
        welcome_text.value = new_content
        page.update()

    # Add Text widget to the page
    page.add(welcome_text)

    # Call the function to update content on app start
    update_content()

ft.app(target=main)
FeodorFitsner commented 8 months ago

@MaxXilon I removed that code around Flet's socket (you shouldn't mess with it really) and it worked like a charm:

My main.py:

import flet as ft
import requests

def get_remote_content():
    # The URL with the actual endpoint
    response = requests.get("https://postman-echo.com/get?name=John%20Smith")
    content = response.json()
    return content["args"]["name"]

def main(page: ft.Page):
    # Create a Text widget with a default text
    welcome_text = ft.Text(value="Hello", size=20)

    # Function to update Text widget content
    def update_content():
        # Fetch the updated content
        new_content = get_remote_content()
        # Update the Text widget with the new content
        welcome_text.value = new_content
        page.update()

    # Add Text widget to the page
    page.add(ft.SafeArea(welcome_text))

    # Call the function to update content on app start
    update_content()

ft.app(target=main)

requirements.txt:

flet
requests

Command to build:

flet build apk

Flet version: 0.19.0

Result:

Screenshot_1705618656

@User777ll Please provide a full and working app to reproduce the issue.

FeodorFitsner commented 8 months ago

I've managed to reproduce "SocketException: Failed to create server socket (OS Error: File exists with given unix domain address), address = stdout.sock, port = 0" issue.

Working on the fix!

FeodorFitsner commented 8 months ago

Fixed the app template - the app is not crashing with that error anymore when restarted multiple times. Rebuild your APK with flet build apk to get updated template.

MaxXilon commented 8 months ago

@FeodorFitsner Thanks 😊 it works fine, it no longer displays the error.