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

Floating point exception #4115

Closed
mrunix00 opened this issue Jun 26, 2023 · 9 comments
Closed

Floating point exception #4115

mrunix00 opened this issue Jun 26, 2023 · 9 comments

Comments

@mrunix00
Copy link

Sometimes when I start scrcpy it crashes and prints the following in the terminal:

mrunix@unknown:~$ scrcpy
scrcpy 1.25 <https://github.com/Genymobile/scrcpy>
/usr/share/scrcpy/scrcpy-server: 1 file pushed, 0 skipped. 6.6 MB/s (41650 bytes in 0.006s)
[server] INFO: Device: samsung SM-E225F (Android 13)
INFO: Renderer: opengl
INFO: OpenGL version: 3.3 (Compatibility Profile) Mesa 23.0.2
INFO: Trilinear filtering enabled
INFO: Initial texture: 720x1600
Floating point exception (core dumped)

This happens sometimes, but sometimes it doesn't, I'm not sure how to reproduce this error.

Environment

  • OS: Ubuntu 23.04
  • scrcpy version: 1.25
  • installation method: apt
  • device model: Samsung Galaxy F22
  • Android version: 13
@mrunix00
Copy link
Author

I tried to run scrcpy with gdb and I got this:

(gdb) run
Starting program: /home/mrunix/scrcpy/x/app/scrcpy 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
scrcpy 2.1 <https://github.com/Genymobile/scrcpy>
[New Thread 0x7fffe81ff6c0 (LWP 151165)]
[Detaching after fork from child process 151166]
[Detaching after fork from child process 151167]
DEBUG: ADB device found:
DEBUG:     -->   (usb)  RZ8RB229ELE                     device  SM_E225F
DEBUG: Device serial: RZ8RB229ELE
DEBUG: Using SCRCPY_SERVER_PATH: ./x//server/scrcpy-server
[Detaching after fork from child process 151168]
./x//server/scrcpy-server: 1 file pushed, 0 skipped. 10.8 MB/s (81453 bytes in 0.007s)
[Detaching after fork from child process 151169]
[Detaching after fork from child process 151170]
[New Thread 0x7fffe6b3e6c0 (LWP 151171)]
[New Thread 0x7fffe85866c0 (LWP 151178)]
[server] INFO: Device: [samsung] samsung SM-E225F (Android 13)
[Detaching after fork from child process 151195]
DEBUG: Server connected
DEBUG: Starting controller thread
[New Thread 0x7fffe633d6c0 (LWP 151196)]
DEBUG: Starting receiver thread
[New Thread 0x7fffe5b3c6c0 (LWP 151197)]
[server] DEBUG: Using audio encoder: 'c2.android.opus.encoder'
[server] DEBUG: Using video encoder: 'c2.mtk.avc.encoder'
[New Thread 0x7fffe50f36c0 (LWP 151198)]
[New Thread 0x7fffe48f26c0 (LWP 151199)]
[Thread 0x7fffe48f26c0 (LWP 151199) exited]
[New Thread 0x7fffe48f26c0 (LWP 151200)]
INFO: Renderer: opengl
INFO: OpenGL version: 3.3 (Compatibility Profile) Mesa 23.0.2
INFO: Trilinear filtering enabled
DEBUG: Using SCRCPY_ICON_PATH: app/data/icon.png
DEBUG: Demuxer 'video': starting thread
[New Thread 0x7fffd9fff6c0 (LWP 151201)]
DEBUG: Demuxer 'audio': starting thread
[New Thread 0x7fffd97fe6c0 (LWP 151202)]
INFO: Texture: 720x1600

Thread 1 "scrcpy" received signal SIGFPE, Arithmetic exception.
0x00005555555740e3 in sc_screen_convert_drawable_to_frame_coords (screen=0x555555616220 <scrcpy+320>, x=158, y=298) at ../app/src/screen.c:852
852	   x = (int64_t) (x - screen->rect.x) * w / screen->rect.w;
(gdb) 

I hope this might be helpful

@rom1v
Copy link
Collaborator

rom1v commented Jun 26, 2023

Thank you for your report.

Could you also print the stack trace in your gdb session (after what you did, just execute bt in gdb):

Thread 1 "scrcpy" received signal SIGFPE, Arithmetic exception.
0x00005555555740e3 in sc_screen_convert_drawable_to_frame_coords (screen=0x555555616220 <scrcpy+320>, x=158, y=298) at ../app/src/screen.c:852
852	   x = (int64_t) (x - screen->rect.x) * w / screen->rect.w;
(gdb) Thread 1 "scrcpy" received signal SIGFPE, Arithmetic exception.
0x00005555555740e3 in sc_screen_convert_drawable_to_frame_coords (screen=0x555555616220 <scrcpy+320>, x=158, y=298) at ../app/src/screen.c:852
852	   x = (int64_t) (x - screen->rect.x) * w / screen->rect.w;
(gdb) bt
…

Also, could you please test with these additional logs:

diff --git a/app/src/screen.c b/app/src/screen.c
index 2724a2660..9098830ee 100644
--- a/app/src/screen.c
+++ b/app/src/screen.c
@@ -209,6 +209,8 @@ sc_screen_update_content_rect(struct sc_screen *screen) {
     int dh;
     SDL_GL_GetDrawableSize(screen->window, &dw, &dh);
 
+    LOGI("=== dw=%d dh=%d", dw, dh);
+
     struct sc_size content_size = screen->content_size;
     // The drawable size is the window size * the HiDPI scale
     struct sc_size drawable_size = {dw, dh};
@@ -238,6 +240,8 @@ sc_screen_update_content_rect(struct sc_screen *screen) {
                                        / content_size.height;
         rect->x = (drawable_size.width - rect->w) / 2;
     }
+
+    LOGI("=== updated: rect->w=%d rect->h=%d", rect->w, rect->h);
 }
 
 // render the texture to the renderer
@@ -848,6 +852,7 @@ sc_screen_convert_drawable_to_frame_coords(struct sc_screen *screen,
     int32_t w = screen->content_size.width;
     int32_t h = screen->content_size.height;
 
+    LOGI("=== rect.w=%d rect.h=%d", screen->rect.w, screen->rect.h);
 
     x = (int64_t) (x - screen->rect.x) * w / screen->rect.w;
     y = (int64_t) (y - screen->rect.y) * h / screen->rect.h;

@mrunix00
Copy link
Author

First of all, here's the backtrace:

(gdb) bt
#0  0x00005555555740e3 in sc_screen_convert_drawable_to_frame_coords (screen=0x555555616220 <scrcpy+320>, x=2, y=377) at ../app/src/screen.c:852
#1  0x00005555555741eb in sc_screen_convert_window_to_frame_coords (screen=0x555555616220 <scrcpy+320>, x=2, y=377) at ../app/src/screen.c:883
#2  0x000055555556bd14 in sc_input_manager_process_mouse_motion (im=0x555555616290 <scrcpy+432>, event=0x7fffffffdc10) at ../app/src/input_manager.c:566
#3  0x000055555556c824 in sc_input_manager_handle_event (im=0x555555616290 <scrcpy+432>, event=0x7fffffffdc10) at ../app/src/input_manager.c:820
#4  0x000055555557403f in sc_screen_handle_event (screen=0x555555616220 <scrcpy+320>, event=0x7fffffffdc10) at ../app/src/screen.c:838
#5  0x0000555555570665 in event_loop (s=0x5555556160e0 <scrcpy>) at ../app/src/scrcpy.c:183
#6  0x0000555555571f90 in scrcpy (options=0x7fffffffde00) at ../app/src/scrcpy.c:782
#7  0x000055555555cdf5 in main_scrcpy (argc=1, argv=0x7fffffffe068) at ../app/src/main.c:82
#8  0x000055555555ce3b in main (argc=1, argv=0x7fffffffe068) at ../app/src/main.c:93
(gdb) 

Second, here's the output of scrcpy after applying that diff:

mrunix@unknown ~> cat test.txt 
(gdb) run
Starting program: /home/mrunix/scrcpy/x/app/scrcpy 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
scrcpy 2.1 <https://github.com/Genymobile/scrcpy>
[New Thread 0x7fffe81ff6c0 (LWP 259807)]
[Detaching after fork from child process 259808]
[Detaching after fork from child process 259809]
DEBUG: ADB device found:
DEBUG:     -->   (usb)  RZ8RB229ELE                     device  SM_E225F
DEBUG: Device serial: RZ8RB229ELE
DEBUG: Using SCRCPY_SERVER_PATH: ./x/server/scrcpy-server
[Detaching after fork from child process 259810]
./x/server/scrcpy-server: 1 file pushed, 0 skipped. 9.4 MB/s (56955 bytes in 0.006s)
[Detaching after fork from child process 259813]
[Detaching after fork from child process 259814]
[New Thread 0x7fffe6b3e6c0 (LWP 259815)]
[New Thread 0x7fffe85866c0 (LWP 259833)]
[server] INFO: Device: [samsung] samsung SM-E225F (Android 13)
[Detaching after fork from child process 259835]
DEBUG: Server connected
DEBUG: Starting controller thread
[New Thread 0x7fffe633d6c0 (LWP 259836)]
DEBUG: Starting receiver thread
[New Thread 0x7fffe5b3c6c0 (LWP 259837)]
[server] DEBUG: Using audio encoder: 'c2.android.opus.encoder'
[server] DEBUG: Using video encoder: 'c2.mtk.avc.encoder'
[New Thread 0x7fffe50f36c0 (LWP 259842)]
[New Thread 0x7fffe48f26c0 (LWP 259847)]
[Thread 0x7fffe48f26c0 (LWP 259847) exited]
[New Thread 0x7fffe48f26c0 (LWP 259849)]
INFO: Renderer: opengl
INFO: OpenGL version: 3.3 (Compatibility Profile) Mesa 23.0.2
INFO: Trilinear filtering enabled
DEBUG: Using SCRCPY_ICON_PATH: app/data/icon.png
DEBUG: Demuxer 'video': starting thread
[New Thread 0x7fffd9fff6c0 (LWP 259850)]
DEBUG: Demuxer 'audio': starting thread
[New Thread 0x7fffd97fe6c0 (LWP 259851)]
INFO: Texture: 720x1600
[New Thread 0x7fffd8ad06c0 (LWP 259852)]
INFO: === rect.w=0 rect.h=0

Thread 1 "scrcpy" received signal SIGFPE, Arithmetic exception.
0x0000555555574162 in sc_screen_convert_drawable_to_frame_coords (screen=0x555555616220 <scrcpy+320>, x=43, y=361) at ../app/src/screen.c:857
857	  x = (int64_t) (x - screen->rect.x) * w / screen->rect.w;
(gdb)

@rom1v
Copy link
Collaborator

rom1v commented Jun 26, 2023

Perfect, thank you.

So it seems that you receive a mouse event before the window is visible, which is unexpected 😮

Could you please apply this diff in addition to the previous one?

diff --git a/app/src/screen.c b/app/src/screen.c
index 9098830ee..37dbf3faf 100644
--- a/app/src/screen.c
+++ b/app/src/screen.c
@@ -491,6 +491,7 @@ sc_screen_show_initial_window(struct sc_screen *screen) {
         sc_fps_counter_start(&screen->fps_counter);
     }
 
+    LOGI("=== show window");
     SDL_ShowWindow(screen->window);
 }
 

Also, what is the result of scrcpy -v (to check the SDL version)?

@mrunix00
Copy link
Author

mrunix00 commented Jun 26, 2023

That's what I got this time

(gdb) run
Starting program: /home/mrunix/scrcpy/x/app/scrcpy 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
scrcpy 2.1 <https://github.com/Genymobile/scrcpy>
[New Thread 0x7fffe81ff6c0 (LWP 263034)]
[Detaching after fork from child process 263035]
[Detaching after fork from child process 263036]
DEBUG: ADB device found:
DEBUG:     -->   (usb)  RZ8RB229ELE                     device  SM_E225F
DEBUG: Device serial: RZ8RB229ELE
DEBUG: Using SCRCPY_SERVER_PATH: ./x/server/scrcpy-server
[Detaching after fork from child process 263037]
./x/server/scrcpy-server: 1 file pushed, 0 skipped. 8.9 MB/s (56955 bytes in 0.006s)
[Detaching after fork from child process 263038]
[Detaching after fork from child process 263042]
[New Thread 0x7fffe6b3e6c0 (LWP 263043)]
[New Thread 0x7fffe85866c0 (LWP 263046)]
[server] INFO: Device: [samsung] samsung SM-E225F (Android 13)
[Detaching after fork from child process 263062]
DEBUG: Server connected
DEBUG: Starting controller thread
[New Thread 0x7fffe633d6c0 (LWP 263063)]
DEBUG: Starting receiver thread
[New Thread 0x7fffe5b3c6c0 (LWP 263064)]
[server] DEBUG: Using audio encoder: 'c2.android.opus.encoder'
[server] DEBUG: Using video encoder: 'c2.mtk.avc.encoder'
[New Thread 0x7fffe50f36c0 (LWP 263067)]
[New Thread 0x7fffe48f26c0 (LWP 263068)]
[Thread 0x7fffe48f26c0 (LWP 263068) exited]
[New Thread 0x7fffe48f26c0 (LWP 263069)]
INFO: Renderer: opengl
INFO: OpenGL version: 3.3 (Compatibility Profile) Mesa 23.0.2
INFO: Trilinear filtering enabled
DEBUG: Using SCRCPY_ICON_PATH: app/data/icon.png
DEBUG: Demuxer 'video': starting thread
[New Thread 0x7fffd9fff6c0 (LWP 263070)]
DEBUG: Demuxer 'audio': starting thread
[New Thread 0x7fffd97fe6c0 (LWP 263071)]
INFO: Texture: 720x1600
INFO: === show window
[New Thread 0x7fffccdff6c0 (LWP 263072)]
INFO: === rect.w=0 rect.h=0

Thread 1 "scrcpy" received signal SIGFPE, Arithmetic exception.
0x000055555557417b in sc_screen_convert_drawable_to_frame_coords (screen=0x555555616220 <scrcpy+320>, x=212, y=523) at ../app/src/screen.c:858
858	   x = (int64_t) (x - screen->rect.x) * w / screen->rect.w;
(gdb) 

And this is the output of scrcpy -v:

scrcpy 2.1 <https://github.com/Genymobile/scrcpy>

Dependencies (compiled / linked):
 - SDL: 2.26.3 / 2.26.3
 - libavcodec: 59.37.100 / 59.37.100
 - libavformat: 59.27.100 / 59.27.100
 - libavutil: 57.28.100 / 57.28.100
 - libavdevice: 59.7.100 / 59.7.100
 - libusb: - / 1.0.26

Oh... another thing, I didn't have this problem on Gnome, this problem only started after I switched to sway wm.

@mrunix00
Copy link
Author

There's a weird thing and I'm not sure if this problem is related to scrcpy, when scrcpy throws a floating point exception while running in gdb its execution gets paused, when I leave it like that for about a minute my phone's screen turns black then it reboots itself.

@rom1v
Copy link
Collaborator

rom1v commented Jun 27, 2023

this problem only started after I switched to sway wm.

I tested on sway, I could not get a FloatingPointException.

My guess is that the SDL_WINDOWEVENT_EXPOSED and SDL_WINDOWEVENT_SIZE_CHANGED are not sent, so the "rect" coordinates are never computed:

diff --git a/app/src/screen.c b/app/src/screen.c
index 2724a2660..59e82fe62 100644
--- a/app/src/screen.c
+++ b/app/src/screen.c
@@ -745,9 +745,11 @@ sc_screen_handle_event(struct sc_screen *screen, const SDL_Event *event) {
             }
             switch (event->window.event) {
                 case SDL_WINDOWEVENT_EXPOSED:
+                    LOGI("===== SDL_WINDOWEVENT_EXPOSED");
                     sc_screen_render(screen, true);
                     break;
                 case SDL_WINDOWEVENT_SIZE_CHANGED:
+                    LOGI("====== SDL_WINDOWEVENT_SIZE_CHANGED");
                     sc_screen_render(screen, true);
                     break;
                 case SDL_WINDOWEVENT_MAXIMIZED:

Could you confirm?

I think this should fix the problem:

diff --git a/app/src/screen.c b/app/src/screen.c
index 2724a2660..9326f927c 100644
--- a/app/src/screen.c
+++ b/app/src/screen.c
@@ -488,6 +488,7 @@ sc_screen_show_initial_window(struct sc_screen *screen) {
     }
 
     SDL_ShowWindow(screen->window);
+    sc_screen_update_content_rect(screen);
 }
 
 void

There's a weird thing and I'm not sure if this problem is related to scrcpy, when scrcpy throws a floating point exception while running in gdb its execution gets paused, when I leave it like that for about a minute my phone's screen turns black then it reboots itself.

Wild guess: since the scrcpy client does not consume the stream on the socket, at some point the device blocks to write on the socket, so it does not consume the MediaCodec output (but the MediaCodec still receives frames as input), so it probably accumulates new frames internally, causing an out-of-memory after some time.

@mrunix00
Copy link
Author

mrunix00 commented Jun 27, 2023

I tested on sway, I could not get a FloatingPointException.

As I said before it doesn't happen always, sometimes it's hard to reproduce, but my guess is that this problem is more likely to happen when my laptop is under high load (it's an 11 years old machine btw).

Anyway, here's the output with the first patch:

mrunix@unknown ~/scrcpy (master)> ./run ./x
scrcpy 2.1 <https://github.com/Genymobile/scrcpy>
DEBUG: ADB device found:
DEBUG:     -->   (usb)  RZ8RB229ELE                     device  SM_E225F
DEBUG: Device serial: RZ8RB229ELE
DEBUG: Using SCRCPY_SERVER_PATH: ./x/server/scrcpy-server
./x/server/scrcpy-server: 1 file pushed, 0 skipped. 10.5 MB/s (56955 bytes in 0.005s)
[server] INFO: Device: [samsung] samsung SM-E225F (Android 13)
DEBUG: Server connected
DEBUG: Starting controller thread
DEBUG: Starting receiver thread
[server] DEBUG: Using audio encoder: 'c2.android.opus.encoder'
[server] DEBUG: Using video encoder: 'c2.mtk.avc.encoder'
INFO: Renderer: opengl
INFO: OpenGL version: 3.3 (Compatibility Profile) Mesa 23.0.2
INFO: Trilinear filtering enabled
DEBUG: Using SCRCPY_ICON_PATH: app/data/icon.png
DEBUG: Demuxer 'video': starting thread
DEBUG: Demuxer 'audio': starting thread
INFO: Texture: 720x1600
INFO: === show window
INFO: === rect.w=0 rect.h=0
./run: line 25: 310827 Floating point exception(core dumped) SCRCPY_ICON_PATH="app/data/icon.png" SCRCPY_SERVER_PATH="$BUILDDIR/server/scrcpy-server" "$BUILDDIR/app/scrcpy" "$@"
[server] DEBUG: Controller stopped
[server] DEBUG: Device message sender stopped
[server] DEBUG: Screen streaming stopped
mrunix@unknown ~/scrcpy (master) [SIGFPE]> [server] DEBUG: Audio encoder stopped

I think your guess is right, for the second patch I think I need more testing to confirm that it fixed the issue, thanks

rom1v added a commit that referenced this issue Jun 27, 2023
On sway (a window manager), SDL_WINDOWEVENT_EXPOSED and
SDL_WINDOWEVENT_SIZE_CHANGED might not be called before a mouse event is
triggered. As a consequence, the "content rectangle" might not be
initialized when the mouse event is processed, causing a division by
zero.

To avoid the problem, initialize the content rect immediately when the
window is shown.

Fixes #4115 <#4115>
@mrunix00
Copy link
Author

I tested scrcpy and I can confirm that the patch fixed this issue, thanks for help

rom1v added a commit that referenced this issue Jun 29, 2023
On sway (a window manager), SDL_WINDOWEVENT_EXPOSED and
SDL_WINDOWEVENT_SIZE_CHANGED might not be called before a mouse event is
triggered. As a consequence, the "content rectangle" might not be
initialized when the mouse event is processed, causing a division by
zero.

To avoid the problem, initialize the content rect immediately when the
window is shown.

Fixes #4115 <#4115>
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