You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by GitBox <gi...@apache.org> on 2022/07/13 01:58:46 UTC

[GitHub] [incubator-brpc] liu-meng-06 opened a new issue, #1835: potobufTojson Exception

liu-meng-06 opened a new issue, #1835:
URL: https://github.com/apache/incubator-brpc/issues/1835

   http client:
   
   ``` c++
   _channel.Init(_host.c_str(), _port, &channel_options)
   
   int send(
       const google::protobuf::Message *req, google::protobuf::Message *resp,
       google::protobuf::Closure *done) {
       try {
               _channel.CallMethod(nullptr, &_cntl, req, resp, done);
       } catch (...) {
       }
   }
   ```
   
   stack
   
   ``` c++
   butil::iobuf::acquire_tls_block iobuf.cpp:455
   butil::IOBufAsZeroCopyOutputStream::Next iobuf.cpp:1930
   json2pb::ZeroCopyStreamWriter::AcquireNextBuf zero_copy_stream_writer.h:95
   json2pb::ZeroCopyStreamWriter::Put zero_copy_stream_writer.h:56
   butil::rapidjson::Writer::WriteStartObject writer.h:312
   butil::rapidjson::Writer::StartObject writer.h:137
   json2pb::PbToJsonConverter::Convert<…> pb_to_json.cpp:106
   json2pb::ProtoMessageToJsonStream<…> pb_to_json.cpp:307
   json2pb::ProtoMessageToJson pb_to_json.cpp:339
   brpc::policy::SerializeHttpRequest http_rpc_protocol.cpp:528
   brpc::Channel::CallMethod channel.cpp:485
   lsbf::HttpClient::send HttpClient.cpp:51
   lsbf::ClientVehicleStatusPareser::forward ClientDataParser.cpp:120
   lsbf::ClientVehicleStatusPareser::parse ClientDataParser.cpp:86
   lsbf::TcpServer::<lambda>::<lambda>::operator()(void) const TcpServer.cpp:262
   std::_Function_handler::_M_invoke(const std::_Any_data &) std_function.h:316
   std::function::operator()() const std_function.h:706
   boost::asio::asio_handler_invoke<std::function<void ()> >(std::function<void ()>&, ...) handler_invoke_hook.hpp:88
   boost_asio_handler_invoke_helpers::invoke<…>(std::function<…> &, std::function<…> &) handler_invoke_helpers.hpp:54
   boost::asio::detail::executor_op::do_complete(void *, boost::asio::detail::scheduler_operation *, const boost::system::error_code &, unsigned long) executor_op.hpp:70
   boost::asio::detail::scheduler_operation::complete scheduler_operation.hpp:40
   boost::asio::detail::scheduler::do_run_one scheduler.ipp:481
   boost::asio::detail::scheduler::run scheduler.ipp:204
   boost::asio::thread_pool::thread_function::operator() thread_pool.ipp:39
   boost::asio::detail::posix_thread::func::run posix_thread.hpp:86
   boost::asio::detail::boost_asio_detail_posix_thread_function posix_thread.ipp:74
   start_thread 0x00007ffff7bbb6db
   clone 0x00007ffff512e61f
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


[GitHub] [incubator-brpc] liu-meng-06 commented on issue #1835: potobufTojson Exception

Posted by GitBox <gi...@apache.org>.
liu-meng-06 commented on issue #1835:
URL: https://github.com/apache/incubator-brpc/issues/1835#issuecomment-1185534843

   > 
   
   - 代码
   
   ``` c++
   #include "test.pb.h"
   #include <brpc/channel.h>
   #include <butil/logging.h>
   #include <memory>
   #include <boost/asio/thread_pool.hpp>
   #include <boost/asio/post.hpp>
   
   #define HOST "127.0.0.1"
   #define PORT (8009)
   
   class Done : public google::protobuf::Closure {
   public:
       explicit Done(int idx)
           : _idx(idx) {}
       ~Done() override = default;
   
       void Run() override {
           LOG(DEBUG) << "done, idx = " << _idx;
           delete this;
       }
   
   private:
       int _idx;
   };
   
   
   static int send(
       const std::string &urlPath, const google::protobuf::Message *req,
       google::protobuf::Message *resp, google::protobuf::Closure *done) {
       brpc::Controller cntl;
       brpc::Channel channel;
       cntl.http_request().uri() = urlPath;
       cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
       brpc::ChannelOptions channel_options;
       channel_options.protocol = brpc::PROTOCOL_HTTP;
       channel_options.timeout_ms = 10000;
   
       try {
           if (channel.Init(HOST, PORT, &channel_options) != 0) {
               LOG(ERROR) << "Fail to init channel ";
               return -1;
           }
   
           channel.CallMethod(nullptr, &cntl, req, resp, done);
           if (!cntl.Failed()) {
               return -1;
           }
           LOG(WARNING) << "call " << cntl.http_request().uri()
                        << " failed : " << cntl.ErrorText();
       } catch (...) {
           LOG(DEBUG) << "channel init failed, address = ";
           return -1;
       }
       return 0;
   }
   
   int main() {
   
       boost::asio::thread_pool pool(5);
   
       for (int i = 0; i< 20; ++i) {
           boost::asio::post(pool, [=]() {
               // 如果不添加到 thread 中,运行没有问题
               // 或者添加到线程池,请求 ”/foo2“ 时,done = nullptr 运行也没有问题
               LOG(DEBUG) << "i = " << i;
               test::foo::FooRequest1 request1;
               test::foo::FooResponse1 response1;
               request1.set_id("foo2");
               request1.set_name("张三");
               request1.set_age(20);
               send("/foo1", &response1, &response1, nullptr);
   
               test::foo::FooRequest2 request2;
               test::foo::FooResponse2 response2;
               request2.set_id("foo2");
               request2.set_state(1);
               auto *done = new Done(i);
               send("/foo2", &response2, &response2, done);
   
           });
           usleep(100000);
       }
   
       pool.join();
   
       return 0;
   }
   
   ```
   
   - proto
   
   ``` proto
   syntax = "proto2";
   
   package test.foo;
   
   option  cc_enable_arenas = true;
   
   message FooRequest1 {
     optional string id = 1;
     optional string name = 2;
     optional int32 age = 3;
   }
   
   message FooResponse1 {}
   
   message FooRequest2 {
     optional string id = 1;
     optional int32 state = 2;
   }
   
   message FooResponse2 {}
   
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


[GitHub] [incubator-brpc] liu-meng-06 commented on issue #1835: potobufTojson Exception

Posted by GitBox <gi...@apache.org>.
liu-meng-06 commented on issue #1835:
URL: https://github.com/apache/incubator-brpc/issues/1835#issuecomment-1182806020

   问题的复现方式:
   
   在一个线程中先构造一个 `http client` ,调用 `CallMethod(nullptr, &_cntl, req, resp, nullptr);`
   执行完成之后,又构造一个 `http client`, 调用 `CallMethod(nullptr, &_cntl, req, resp, done);`,此时 done 不为空。
   
   当我把第二次构造的 `http client` 的 `done` 也传入空时,异常就没再出现。
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


[GitHub] [incubator-brpc] wasphin commented on issue #1835: potobufTojson Exception

Posted by GitBox <gi...@apache.org>.
wasphin commented on issue #1835:
URL: https://github.com/apache/incubator-brpc/issues/1835#issuecomment-1186224878

   `done` 不为空情况下是异步调用, 在这个例子中, `CallMethod` 直接就返回了, 导致 `cntl` 释放, 回复时非法访问 `cntl` 导致各种异常.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


[GitHub] [incubator-brpc] liu-meng-06 commented on issue #1835: potobufTojson Exception

Posted by GitBox <gi...@apache.org>.
liu-meng-06 commented on issue #1835:
URL: https://github.com/apache/incubator-brpc/issues/1835#issuecomment-1183854668

   > > 问题的复现方式:
   > > 在一个线程中先构造一个 `http client` ,调用 `CallMethod(nullptr, &_cntl, req, resp, nullptr);` 执行完成之后,又构造一个 `http client`, 调用 `CallMethod(nullptr, &_cntl, req, resp, done);`,此时 done 不为空。
   > > 当我把第二次构造的 `http client` 的 `done` 也传入空时,异常就没再出现。
   > 
   > 异步调用的话,检查下是否在done中使用了controller,有可能这个controller在返回response后提前被析构了。
   
   `done` 继承 `google::protobuf::Closure` , 里边没有使用到 `controller`。`new` 了一个 `done`, 在它的 `run()` 最后 执行了 `delete this` 对它进行了释放。


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


[GitHub] [incubator-brpc] lorinlee commented on issue #1835: potobufTojson Exception

Posted by GitBox <gi...@apache.org>.
lorinlee commented on issue #1835:
URL: https://github.com/apache/incubator-brpc/issues/1835#issuecomment-1183913048

   @liu-meng-06 请问brpc是用了哪个版本,linux发行版是哪个,以及用了什么版本的编译器。如果不是最新版本的brpc,建议先升级到最新尝试一下
   
   另外如果用http client的example是可复现的,辛苦提供一个完整的最小可复现代码片段吧


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


[GitHub] [incubator-brpc] lorinlee commented on issue #1835: potobufTojson Exception

Posted by GitBox <gi...@apache.org>.
lorinlee commented on issue #1835:
URL: https://github.com/apache/incubator-brpc/issues/1835#issuecomment-1186401815

   @liu-meng-06 可以参考下Client文档的异步访问章节哈,有提到异步调用需要注意response/controller的生命周期。https://github.com/apache/incubator-brpc/blob/master/docs/cn/client.md#%E5%BC%82%E6%AD%A5%E8%AE%BF%E9%97%AE


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


[GitHub] [incubator-brpc] lorinlee closed issue #1835: potobufTojson Exception

Posted by GitBox <gi...@apache.org>.
lorinlee closed issue #1835: potobufTojson Exception
URL: https://github.com/apache/incubator-brpc/issues/1835


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


[GitHub] [incubator-brpc] Huixxi commented on issue #1835: potobufTojson Exception

Posted by GitBox <gi...@apache.org>.
Huixxi commented on issue #1835:
URL: https://github.com/apache/incubator-brpc/issues/1835#issuecomment-1183171323

   > 问题的复现方式:
   > 
   > 在一个线程中先构造一个 `http client` ,调用 `CallMethod(nullptr, &_cntl, req, resp, nullptr);` 执行完成之后,又构造一个 `http client`, 调用 `CallMethod(nullptr, &_cntl, req, resp, done);`,此时 done 不为空。
   > 
   > 当我把第二次构造的 `http client` 的 `done` 也传入空时,异常就没再出现。
   
   异步调用的话,检查下是否在done中使用了controller,有可能这个controller在返回response后提前被析构了。


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org