Skip to content

Commit

Permalink
fix(odd): pretend to be online all the time (#13746)
Browse files Browse the repository at this point in the history
For some reason, when the ODD starts and the robot boots while not connected to any network, some (some!) requests to localhost will fail. It's really strange. One thing that seems to help is pretending to be online by overriding the navigator.onLine browser api to always return true.

---------

Co-authored-by: Jamey H <[email protected]>
  • Loading branch information
sfoster1 and mjhuff authored Oct 10, 2023
1 parent 47d486c commit dc4fe46
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app-shell-odd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ setup:

.PHONY: clean
clean:
shx rm -rf lib dist
shx rm -rf lib dist opentrons-robot-app.tar.gz

# artifacts
#####################################################################
Expand All @@ -75,7 +75,7 @@ push-ot3: dist-ot3
scp $(if $(ssh_key),-i $(ssh_key)) $(ssh_opts) -r ./opentrons-robot-app.tar.gz root@$(host):
ssh $(if $(ssh_key),-i $(ssh_key)) $(ssh_opts) root@$(host) "mount -o remount,rw / && systemctl stop opentrons-robot-app && rm -rf /opt/opentrons-app && mkdir -p /opt/opentrons-app"
ssh $(if $(ssh_key),-i $(ssh_key)) $(ssh_opts) root@$(host) "tar -xvf opentrons-robot-app.tar.gz -C /opt/opentrons-app/ && mount -o remount,ro / && systemctl start opentrons-robot-app && rm -rf opentrons-robot-app.tar.gz"
rm -rf opentrons-robot-app.tar.gz


# development
#####################################################################
Expand Down
8 changes: 7 additions & 1 deletion app/src/App/OnDeviceDisplayApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ import { useCurrentRunRoute, useProtocolReceiptToast } from './hooks'

import { OnDeviceDisplayAppFallback } from './OnDeviceDisplayAppFallback'

import { hackWindowNavigatorOnLine } from './hacks'

import type { Dispatch } from '../redux/types'
import type { RouteProps } from './types'

// forces electron to think we're online which means axios won't elide
// network calls to localhost. see ./hacks.ts for more.
hackWindowNavigatorOnLine()

export const onDeviceDisplayRoutes: RouteProps[] = [
{
Component: InitialLoadingScreen,
Expand Down Expand Up @@ -257,7 +263,7 @@ export const OnDeviceDisplayApp = (): JSX.Element => {

// TODO (sb:6/12/23) Create a notification manager to set up preference and order of takeover modals
return (
<ApiHostProvider hostname="localhost">
<ApiHostProvider hostname="127.0.0.1">
<ErrorBoundary FallbackComponent={OnDeviceDisplayAppFallback}>
<Box width="100%" css="user-select: none;">
{isIdle ? (
Expand Down
1 change: 1 addition & 0 deletions app/src/App/__mocks__/hacks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const hackWindowNavigatorOnLine = (): void => {}
16 changes: 16 additions & 0 deletions app/src/App/hacks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// If the system boots while no network connection is available, then some requests to localhost
// hang eternally while connecting (so no request timeouts work either) and things get
// generally weird. Overriding the browser API to pretend to always be "online" fixes this.
// It makes sense; if "onLine" is false, that means that any network call is _guaranteed_ to fail
// so middlewares probably elide them; but we really want it to be true basically always because
// most of what we do is via localhost.
//
// This function is exposed in its own module so it can be mocked in testing
// since jest really doesn't like you doing this.

export const hackWindowNavigatorOnLine = (): void => {
Object.defineProperty(window.navigator, 'onLine', {
get: () => true,

Check warning on line 13 in app/src/App/hacks.ts

View check run for this annotation

Codecov / codecov/patch

app/src/App/hacks.ts#L11-L13

Added lines #L11 - L13 were not covered by tests
})
window.dispatchEvent(new Event('online'))

Check warning on line 15 in app/src/App/hacks.ts

View check run for this annotation

Codecov / codecov/patch

app/src/App/hacks.ts#L15

Added line #L15 was not covered by tests
}
1 change: 1 addition & 0 deletions scripts/setup-global-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jest.mock('../app/src/pages/Labware/helpers/getAllDefs')
jest.mock('../app/src/logger')
jest.mock('../app/src/App/portal')
jest.mock('../app/src/redux/shell/remote')
jest.mock('../app/src/App/hacks')
jest.mock('../app-shell/src/config')
jest.mock('../app-shell/src/log')
jest.mock('../app-shell-odd/src/config')
Expand Down

0 comments on commit dc4fe46

Please sign in to comment.