Skip to content

Commit

Permalink
fix(speech/api): prevent segfault when arguments aren't parsed correc…
Browse files Browse the repository at this point in the history
…tly (#254)
  • Loading branch information
alevenberg authored Oct 4, 2023
1 parent f5e6461 commit 9e39e76
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
11 changes: 6 additions & 5 deletions speech/api/streaming_transcribe_coroutines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using RecognizeStream = ::google::cloud::AsyncStreamingReadWriteRpc<
speech::v1::StreamingRecognizeResponse>;

auto constexpr kUsage = R"""(Usage:
streaming_transcribe_singlethread [--bitrate N] audio.(raw|ulaw|flac|amr|awb)
streaming_transcribe_coroutines [--bitrate N] audio.(raw|ulaw|flac|amr|awb)
)""";

// Print the responses as they are received.
Expand Down Expand Up @@ -114,15 +114,16 @@ int main(int argc, char* argv[]) try {
// operations, and dedicate a thread to it.
g::CompletionQueue cq;
auto runner = std::thread{[](auto cq) { cq.Run(); }, cq};
// Shutdown the completion queue and join the thread.
std::shared_ptr<void> auto_shutdown(nullptr, [&](void*) {
cq.Shutdown();
runner.join();
});

// Run a streaming transcription. Note that `.get()` blocks until it
// completes.
auto status = StreamingTranscribe(cq, ParseArguments(argc, argv)).get();

// Shutdown the completion queue.
cq.Shutdown();
runner.join();

if (!status.ok()) {
std::cerr << "Error in transcribe stream: " << status << "\n";
return 1;
Expand Down
9 changes: 5 additions & 4 deletions speech/api/streaming_transcribe_singlethread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ int main(int argc, char** argv) try {
// operations, and dedicate a thread to it.
g::CompletionQueue cq;
auto runner = std::thread{[](auto cq) { cq.Run(); }, cq};
// Shutdown the completion queue and join the thread.
std::shared_ptr<void> auto_shutdown(nullptr, [&](void*) {
cq.Shutdown();
runner.join();
});

// Create a Speech client with the default configuration.
auto client = speech::SpeechClient(speech::MakeSpeechConnection(
Expand All @@ -165,10 +170,6 @@ int main(int argc, char** argv) try {
auto handler = Handler::Create(cq, ParseArguments(argc, argv));
auto status = handler->Start(client).get();

// Shutdown the completion queue
cq.Shutdown();
runner.join();

if (!status.ok()) {
std::cerr << "Error in transcribe stream: " << status << "\n";
return 1;
Expand Down

0 comments on commit 9e39e76

Please sign in to comment.