kabucom/kabusapi

【不具合】Rest API(注文発注)呼び出し時に例外が発生する

Opened this issue · 2 comments

■[必須] 不具合事象(概要)
Rest API(注文発注)利用時に例外が発生することがあります。
下記のAPI利用時に、応答がなく、20秒後、例外(System.ServiceModel.CommunicationException)によって、制御が戻ってきました。

・利用しているAPI
http://localhost:18080/kabusapi/sendorder

VPS環境にて1か月ほど運用しておりますが、これまでこのような事象が発生することはありませんでした。
Rest APIと並行して、Push APIで50銘柄 の情報を入手しておりますが、本エラーが発生時も、
Push APIは動作していることから、ネットワークは、接続されているようです。
なお、本エラー発生後のsendorder の呼び出しでは、
・Code":4001007,"Message":"ログイン認証エラー"
が返るようになりました。

このような事象は確認されておりますでしょうか?
ネットワークの問題により防ぐことができない場合であっても、推奨される復旧方法を教えてほしいです。

なお、下記の事象が関連しそうなので、参照しておきます

(参照)
#459

■[必須] 本来想定されるアクションと事象
注文発注の呼び出しに成功すること。

■[必須] 不備発生時刻(常時の場合は、常時で記載ください)
2024年5月10日 10:30:04.444

■[任意] お使いの環境(OS、Excelのバージョン等)、サンプル(Macro、アドインのバージョン)
OS:WindowsServer2022
開発環境:Visual Studio 2022 (C#)

CSharp_sample.sln をベースに、APIを呼び出しております。

■[任意] 不備部分のログ

①APIログ
ーーーー
0:10:39.1569,I,4964,0007 【SendOrder】({"Password":"","Symbol":"6702","Exchange":1,"SecurityType":1,"Side":"1","CashMargin":3,"MarginTradeType":"3","DelivType":2,"AccountType":4,"Qty":100,"ClosePositions":[{"HoldID":"E2024051001N18","Qty":100}],"FrontOrderType":10,"Price":0.0,"ExpireDay":0})
10:10:39.2505,I,4964,0007 【SendOrder】({"Result":0,"OrderId":"20240510A01N46998390"})
10:10:39.5791,I,4964,0007 【Orders】(2,20240510A01N46998390,,true,,,,)
10:10:39.5791,I,4964,0007 【Orders】([{"CashMargin":3,"MarginTradeType":3,"MarginPremium":null,"ID":"20240510A01N46998390","State":5,"OrderState":5,"OrdType":1,"RecvTime":"2024-05-10T10:10:39.0735672+09:00","Symbol":"6702","SymbolName":"富士通","Exchange":1,"ExchangeName":"東証プ","Price":0.0,"OrderQty":100.0,"CumQty":100.0,"Side":"1","AccountType":4,"DelivType":0,"ExpireDay":20240510,"Details":[{"SeqNum":1,"ID":"20240510A01N46998390","RecType":1,"ExchangeID":null,"State":3,"TransactTime":"2024-05-10T10:10:39.0735672+09:00","OrdType":1,"Price":0.0,"Qty":100.0,"ExecutionID":null,"ExecutionDay":null,"DelivDay":20240514,"Commission":0.0,"CommissionTax":0.0},{"SeqNum":4,"ID":"20240510B01N46998391","RecType":4,"ExchangeID":"1F111600010796","State":3,"TransactTime":"2024-05-10T10:10:39.254302+09:00","OrdType":1,"Price":0.0,"Qty":100.0,"ExecutionID":null,"ExecutionDay":null,"DelivDay":20240514,"Commission":0.0,"CommissionTax":0.0},{"SeqNum":5,"ID":"20240510E01N46998392","RecType":8,"ExchangeID":"6810","State":3,"TransactTime":"2024-05-10T10:10:39.254394+09:00","OrdType":0,"Price":2402.5,"Qty":100.0,"ExecutionID":"E2024051001ND6","ExecutionDay":"2024-05-10T10:10:39.254394+09:00","DelivDay":20240514,"Commission":0.0,"CommissionTax":0.0}]}])
10:30:04.4444,I,4964,0007 【SendOrder】({"Password":"
","Symbol":"7011","Exchange":1,"SecurityType":1,"Side":1,"CashMargin":2,"MarginTradeType":3,"DelivType":0,"AccountType":4,"Qty":100,"FrontOrderType":10,"Price":0.0,"ExpireDay":0})
ーーーー

②デバッグログ(Acountは、消しています)
ーーーー
10:30:04.5665,I,4964,0007 ワンショットチェック : 7011/T/三菱重工業/1 : amount=200, qty=
10:30:04.5665,I,4964,0007 KabuSApiManager.PreRelationCheck=CHECK_OK
10:30:04.6290,I,4964,0007 発注パラメータ:[新規] 成行
10:30:04.6290,I,4964,0007 OrderManager.Order Start:OrderParam: Account=XXXXXXXX,CheckMode=1,Symbol=7011,Exchange=1,SecurityType=1,Side=1,CashMargin=2,MarginTradeType=3,OrderCapacity=,Price=0,Qty=100,OrdType=1,AdjustPrice=False,SellingShot=DelivType=0,FundType=11,AccountType=4,EffectiveDay=20240510,ExpireDay=20240510,Trigger=[],OrderReferenceCommand=[],ClosePositions=[],PriceType=0,TimeInForce=0,ExpireSession=0,Password=xxx,FrontInfo=0,UsePoint=FrontInfo=[Key=FrontChannelID/Value=240,Key=MarginPremium/Value=0,Key=Trader/Value=000000A7,Key=Channel/Value=0,Key=ClientCode/Value=203.137.100.208,Key=Branch/Value=000,],SettCurrency=0
10:30:24.8735,E,4964,0007 リクエスト処理中のエラー
System.ServiceModel.CommunicationException: https://web.r10.kabu.co.jp/TA/OrderService.svc/b に対する HTTP 応答の受信中にエラーが発生しました。この原因としては、サービス エンドポイント バインドが HTTP プロトコルを使用していないことが考えられます。また別の原因として、HTTP 要求コンテキストがサーバーによって中止された可能性もあります (サーバーがシャットダウンした場合など)。詳細についてはサーバー ログを参照してください。 ---> System.Net.WebException: 接続が切断されました: 受信時に予期しないエラーが発生しました。 ---> System.IO.IOException: 転送接続からデータを読み取れません: 接続済みの呼び出し先が一定の時間を過ぎても正しく応答しなかったため、接続できませんでした。または接続済みのホストが応答しなかったため、確立された接続は失敗しました。。 ---> System.Net.Sockets.SocketException: 接続済みの呼び出し先が一定の時間を過ぎても正しく応答しなかったため、接続できませんでした。または接続済みのホストが応答しなかったため、確立された接続は失敗しました。
場所 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- 内部例外スタック トレースの終わり ---
場所 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
場所 System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
場所 System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
場所 System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
場所 System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
場所 System.Net.TlsStream.Read(Byte[] buffer, Int32 offset, Int32 size)
場所 System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
場所 System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- 内部例外スタック トレースの終わり ---
場所 System.Net.HttpWebRequest.GetResponse()
場所 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- 内部例外スタック トレースの終わり ---

Server stack trace:
場所 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
場所 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
場所 System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
場所 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
場所 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
場所 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
場所 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
場所 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
場所 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
場所 TA.KC.IOrderService.Order(AuthenticationHeader Header, OrderParam Param)
場所 aun.f(OrderParam A_0, b A_1)
場所 oo.d(ApiSendOrderParam A_0, String A_1, Boolean A_2)
場所 ur.b(HttpListenerRequest A_0, HttpListenerResponse A_1, Boolean A_2)
場所 ur.b(HttpListenerContext A_0)
10:30:27.2508,W,4964,0030 キャッシュ更新チェックに失敗しました。
System.ServiceModel.EndpointNotFoundException: メッセージを受信できる https://web.r10.kabu.co.jp/TA/MasterService.svc/b でリッスンしているエンドポイントがありませんでした。これは一般に、アドレスまたは SOAP アクションが正しくない場合に発生します。詳細については、InnerException を参照してください (ある場合)。 ---> System.Net.WebException: リモート サーバーに接続できません。 ---> System.Net.Sockets.SocketException: 接続済みの呼び出し先が一定の時間を過ぎても正しく応答しなかったため、接続できませんでした。または接続済みのホストが応答しなかったため、確立された接続は失敗しました。 194.223.146.44:443
場所 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
場所 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- 内部例外スタック トレースの終わり ---
場所 System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
場所 System.Net.HttpWebRequest.GetRequestStream()
場所 System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream()
--- 内部例外スタック トレースの終わり ---

Server stack trace:
場所 System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream()
場所 System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
場所 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout)
場所 System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
場所 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
場所 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
場所 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
場所 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
場所 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
場所 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
場所 TA.KC.IMasterService.GetCacheTime(AuthenticationHeader Header)
場所 aq0.d(Object A_0)
10:30:29.2330,E,4964,0006 認証更新時例外エラー発生。Account=XXXXXXXX
System.ServiceModel.EndpointNotFoundException: メッセージを受信できる https://web.r10.kabu.co.jp/TA/AccountService.svc/b でリッスンしているエンドポイントがありませんでした。これは一般に、アドレスまたは SOAP アクションが正しくない場合に発生します。詳細については、InnerException を参照してください (ある場合)。 ---> System.Net.WebException: リモート サーバーに接続できません。 ---> System.Net.Sockets.SocketException: 接続済みの呼び出し先が一定の時間を過ぎても正しく応答しなかったため、接続できませんでした。または接続済みのホストが応答しなかったため、確立された接続は失敗しました。 194.223.146.44:443
場所 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
場所 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- 内部例外スタック トレースの終わり ---
場所 System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
場所 System.Net.HttpWebRequest.GetRequestStream()
場所 System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream()
--- 内部例外スタック トレースの終わり ---

Server stack trace:
場所 System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream()
場所 System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
場所 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout)
場所 System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
場所 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
場所 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
場所 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
場所 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
場所 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
場所 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
場所 TA.KC.IAccountService.KeepSession(AuthenticationHeader Header)
場所 ap5.h(AuthenticationHeader oHeader)
場所 ap5.h(Object A_0)
10:30:29.2330,I,4964,0006 認証更新エラー Code=10010
10:30:29.2330,I,4964,0006 カブステAPIクリア
ーーーー

当件ですが、参照先を記載して頂いているのと同事象と推測されます。
情報系に関してはセッション切断後に接続のリトライが行われているため、切断が発生しても環境の復活と併せて再接続が行われますが、執行系(注文系、注文関連情報の取得系)に関しては、ログインしていることが前提となるため、自動復旧が行われません。
こちらはカブステーション自体が当該仕様となります。

おそらくは、この事象が発生した際はカブステ自体がログアウト状態になっておりませんでしょうか。

回答ありがとうございます。

#459と同様の原因ということで、ネットワークの一時的な不調により、
このような現象が発生したと考えてよろしいでしょうか?

なお、常時監視しているわけではないので、カブステ自体の状態は確認することはできませんが、
復旧のためにはカブステ自体の再ログインが必要ということでしょうか?
(※トークンの再発行で復旧しないですかね??)