birdofpreyru / react-native-static-server

Embedded HTTP server for React Native
https://dr.pogodin.studio/docs/react-native-static-server
Other
142 stars 22 forks source link

Library may break if JS layer is reloaded by RN while the server is active #56

Closed dagouzhi closed 1 year ago

dagouzhi commented 1 year ago

If you call codePush.restartapp(); An error occurs when server.start() is called

another server instance is active

react-native-code-push@8.0.2 @dr.pogodin/react-native-static-server@0.8.0

birdofpreyru commented 1 year ago

Hey @dagouzhi , technically what happens here, I believe, codePush.restartapp() reloads JS layer of the library, it does not stop the actual server in the Native layer, thus the error when reloaded JS layer attempts to start a fresh server, without killing the old one first.

A quick workaround for you — be sure to call server.stop() prior to the restart.

A proper fix will be to hook it up to automatically kill the server in native layer when such reload of JS happens. At the moment, I am not sure whether it can be done in JS layer (which would be perfect), I think I know how to do within the native layer on Android, but then not sure how to do it in the native layer for other platforms. Thus, one way or another, I'll need some time to investigate and fix it. On what platform have you encountered this problem?

dagouzhi commented 1 year ago

@birdofpreyru I could try to do this, but hopefully it would be better to have a static method that kills all services, or to call the start method with a configuration that forces it to start, even if there were previously active services

birdofpreyru commented 1 year ago

but hopefully it would be better to have a static method that kills all services, or to call the start method with a configuration that forces it to start, even if there were previously active services

It will work great for your scenario, but I am afraid in other scenarios it will open doors to hard-to-triage bugs. So, the best permanent solution is just to figure out all situations when the current server should be killed automatically, and do just it.

birdofpreyru commented 1 year ago

Let’s keep it open, I need it as a reminder to look into it.

birdofpreyru commented 1 year ago

For the record — the issue I am tracking and intend to solve here is the following — when JS layer in RN is reloaded (e.g. in dev mode, or by some other setup that does such reload) the library may break with "another server instance is active" error because the atomicity of server start-up and shut-down operations is currently enforced in JS layer, and thus breaks across JS reload. In other words, when .stop() is called on a server instance just before JS reload (it still should be called explicitly, or implicitly due to the "Stop in Background" feature), and then .start() is called on a server instance just after the reload, it is now possible that in the native layer the start of new server instance happens prior the shutdown and clean-up of the previous server instance has completed.