-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nudge the user to kill programs using excessive CPU
- Loading branch information
1 parent
f9824eb
commit 618740b
Showing
13 changed files
with
280 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'json' | ||
|
||
require 'spec_helper' | ||
require 'support/editor' | ||
require 'support/playground_actions' | ||
|
||
RSpec.feature "Excessive executions", type: :feature, js: true do | ||
include PlaygroundActions | ||
|
||
before do | ||
visit "/?#{config_overrides}" | ||
editor.set(code) | ||
end | ||
|
||
scenario "a notification is shown" do | ||
within(:header) { click_on("Run") } | ||
within(:notification, text: 'will be automatically killed') do | ||
expect(page).to have_button 'Kill the process now' | ||
expect(page).to have_button 'Allow the process to continue' | ||
end | ||
end | ||
|
||
scenario "the process is automatically killed if nothing is done" do | ||
within(:header) { click_on("Run") } | ||
expect(page).to have_selector(:notification, text: 'will be automatically killed', wait: 2) | ||
expect(page).to_not have_selector(:notification, text: 'will be automatically killed', wait: 4) | ||
expect(page).to have_content("Exited with signal 9") | ||
end | ||
|
||
scenario "the process can continue running" do | ||
within(:header) { click_on("Run") } | ||
within(:notification, text: 'will be automatically killed') do | ||
click_on 'Allow the process to continue' | ||
end | ||
within(:output, :stdout) do | ||
expect(page).to have_content("Exited normally") | ||
end | ||
end | ||
|
||
def editor | ||
Editor.new(page) | ||
end | ||
|
||
def code | ||
<<~EOF | ||
use std::time::{Duration, Instant}; | ||
fn main() { | ||
let start = Instant::now(); | ||
while start.elapsed() < Duration::from_secs(5) {} | ||
println!("Exited normally"); | ||
} | ||
EOF | ||
end | ||
|
||
def config_overrides | ||
config = { | ||
killGracePeriodS: 3.0, | ||
excessiveExecutionTimeS: 0.5, | ||
} | ||
|
||
"whte_rbt.obj=#{config.to_json}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { TypedStartListening, createListenerMiddleware, isAnyOf } from '@reduxjs/toolkit'; | ||
|
||
import { AppDispatch } from './configureStore'; | ||
import { State } from './reducers'; | ||
import { | ||
allowLongRun, | ||
wsExecuteEnd, | ||
wsExecuteKill, | ||
wsExecuteStatus, | ||
} from './reducers/output/execute'; | ||
import { | ||
currentExecutionSequenceNumberSelector, | ||
excessiveExecutionSelector, | ||
killGracePeriodMsSelector, | ||
} from './selectors'; | ||
|
||
export const observer = createListenerMiddleware(); | ||
|
||
type AppStartListening = TypedStartListening<State, AppDispatch>; | ||
const startAppListening = observer.startListening as AppStartListening; | ||
|
||
// Watch for requests chewing up a lot of CPU and kill them unless the | ||
// user deliberately elects to keep them running. | ||
startAppListening({ | ||
matcher: isAnyOf(wsExecuteStatus, allowLongRun, wsExecuteEnd), | ||
effect: async (_, listenerApi) => { | ||
// Just one listener at a time. | ||
listenerApi.unsubscribe(); | ||
|
||
await listenerApi.condition((_, state) => excessiveExecutionSelector(state)); | ||
|
||
// Ensure that we only act on the current execution, not whatever | ||
// is running later on. | ||
const state = listenerApi.getState(); | ||
const gracePeriod = killGracePeriodMsSelector(state); | ||
const sequenceNumber = currentExecutionSequenceNumberSelector(state); | ||
|
||
if (sequenceNumber) { | ||
const killed = listenerApi | ||
.delay(gracePeriod) | ||
.then(() => listenerApi.dispatch(wsExecuteKill(undefined, sequenceNumber))); | ||
|
||
const allowed = listenerApi.condition((action) => allowLongRun.match(action)); | ||
|
||
const ended = listenerApi.condition( | ||
(action) => wsExecuteEnd.match(action) && action.meta.sequenceNumber === sequenceNumber, | ||
); | ||
|
||
await Promise.race([killed, allowed, ended]); | ||
} | ||
|
||
listenerApi.subscribe(); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.