question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(microservice)There is a concurrent bug in microservice.

See original GitHub issue

I’m submitting a…


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

[Nest] 17131   - 2018-3-13 18:56:06   [NestFactory] Starting Nest application...
[Nest] 17131   - 2018-3-13 18:56:06   [InstanceLoader] ApplicationModule dependencies initialized +6ms
[Nest] 17131   - 2018-3-13 18:56:06   [InstanceLoader] CatsModule dependencies initialized +2ms
[Nest] 17131   - 2018-3-13 18:56:06   [InstanceLoader] MicroClientModule dependencies initialized +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [RoutesResolver] CatsController {/cats}: +27ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/, POST} route +1ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/, GET} route +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/micro, GET} route +1ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/micro2, GET} route +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/microBuffer, GET} route +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/:id, GET} route +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [NestApplication] Nest application successfully started +1ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:196,102,138,162,96,result:531 +28628ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:149,191,43,199,184,result:531 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:172,173,129,100,34,result:531 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:78,73,184,99,46,result:531 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [ClientTCP] Error: write after end
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:139,101,64,195,156,result:632 +9ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:153,109,96,9,105,result:632 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] count:50 +1ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:31,156,77,50,185,result:632 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:128,22,188,27,2,result:632 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:152,69,56,3,181,result:448 +11ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:73,197,170,75,105,result:448 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:38,4,169,178,1,result:656 +6ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:137,174,56,115,25,result:401 +9ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:12,99,37,139,113,result:401 +1ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:191,41,152,122,132,result:401 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:186,106,14,27,186,result:401 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:142,60,16,127,165,result:472 +11ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:136,179,67,184,79,result:472 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:117,89,133,67,95,result:472 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:165,179,192,9,85,result:472 +1ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:132,191,146,85,128,result:567 +11ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:91,34,66,18,29,result:567 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:82,67,30,52,161,result:567 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:189,94,104,137,82,result:567 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:89,143,95,74,126,result:236 +6ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:150,136,38,131,87,result:437 +4ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:77,34,156,55,97,result:437 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:95,197,162,80,178,result:369 +5ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:11,194,108,198,106,result:369 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [ClientTCP] Error: write after end
[Nest] 17131   - 2018-3-13 18:56:34   [ClientTCP] Error: write after end
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:190,94,154,195,148,result:617 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] data:196,41,193,170,151,result:617 +0ms
[Nest] 17131   - 2018-3-13 18:56:34   [ClientTCP] Error: write after end
[Nest] 17131   - 2018-3-13 18:56:34   [ClientTCP] Error: write after end
[Nest] 17131   - 2018-3-13 18:56:34   [ClientTCP] Error: write after end
[Nest] 17131   - 2018-3-13 18:56:34   [ClientTCP] Error: write after end
[Nest] 17131   - 2018-3-13 18:56:34   [ClientTCP] Error: write after end

Expected behavior

[Nest] 17131   - 2018-3-13 18:56:06   [NestFactory] Starting Nest application...
[Nest] 17131   - 2018-3-13 18:56:06   [InstanceLoader] ApplicationModule dependencies initialized +6ms
[Nest] 17131   - 2018-3-13 18:56:06   [InstanceLoader] CatsModule dependencies initialized +2ms
[Nest] 17131   - 2018-3-13 18:56:06   [InstanceLoader] MicroClientModule dependencies initialized +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [RoutesResolver] CatsController {/cats}: +27ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/, POST} route +1ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/, GET} route +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/micro, GET} route +1ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/micro2, GET} route +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/microBuffer, GET} route +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [RouterExplorer] Mapped {/:id, GET} route +0ms
[Nest] 17131   - 2018-3-13 18:56:06   [NestApplication] Nest application successfully started +1ms
[Nest] 17131   - 2018-3-13 18:56:34   [CatsController] count:50 +1ms

Minimal reproduction of the problem with instructions

Client

@Component()
export class MicroClientService {
    @Client({ transport: Transport.TCP, port: 3006 })
    public testMicroClient: ClientProxy

}

@Controller('cats')
export class CatsController {
    logger:Logger;
    count:number;
    constructor(private readonly catsService: CatsService, private readonly microClientService: MicroClientService) {
        this.logger = new Logger("CatsController",true);
        this.count = 0;
    }

    static getRandom():number{
        return Math.floor(Math.random()*200);
    }
    @Get('/micro')
    async testMicroService() {
        const data = [CatsController.getRandom(),CatsController.getRandom(),CatsController.getRandom(),CatsController.getRandom(),CatsController.getRandom()];
        const result = await this.microClientService.testMicroClient.send<number>({cmd: 'sum'}, data).toPromise();
        if(result!=data.reduce((a,b)=>a+b)){
            this.logger.error(`data:${data.toString()},result:${result}`);
        }
        this.count++;
        if(this.count%50==0){
            this.logger.log(`count:${this.count}`);
        }
        return result;
    }
}

Server

  const app = await NestFactory.createMicroservice(ApplicationModule,{
    transport: Transport.TCP,
      port:3006
  });
@Controller()
export class MathController {
  @MessagePattern({ cmd: 'sum' })
  sum(data: number[]): number {
  //  throw new RpcException('~~server error~');
    return (data || []).reduce((a, b) => a + b);
  }
}


test code

function fun(){
    $.ajax({
	url:'http://127.0.0.1:3000/cats/micro',
	method:'GET',
	success: function(res){
		
	},
	error: function(err){
		console.error('err',err);
  }});
}

// !!!!!!
for(var i=0;i<60;i++){fun()}

What is the motivation / use case for changing the behavior?

i think the problem is … https://github.com/nestjs/nest/blob/7bb35c0b387b46a49ce6d5cdaf039bbfb2da7762/src/microservices/server/server.ts#L25-L36 https://github.com/nestjs/nest/blob/7bb35c0b387b46a49ce6d5cdaf039bbfb2da7762/src/microservices/server/server.ts#L34

client: https://github.com/nestjs/nest/blob/7bb35c0b387b46a49ce6d5cdaf039bbfb2da7762/src/microservices/client/client-tcp.ts#L40-L63

https://github.com/nestjs/nest/blob/7bb35c0b387b46a49ce6d5cdaf039bbfb2da7762/src/microservices/client/client-tcp.ts#L58-L59

If i want to re-implement ClientProxy and CustomTransportStrategy will modify server.ts and client-proxy.ts.

i want use my own implement , how can i do? https://github.com/whtiehack/pinus/blob/master/packages/pinus-rpc/lib/rpc-server/acceptors/tcp-acceptor.ts

https://github.com/whtiehack/pinus/blob/master/packages/pinus-rpc/lib/rpc-client/mailboxes/tcp-mailbox.ts

https://github.com/whtiehack/pinus/blob/master/packages/pinus-rpc/lib/util/composer.ts

Environment


Nest version:4.6.6

 
For Tooling issues:
- Node version: 8.9.4 
- Platform:  Mac 

Others:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Mar 14, 2018

Thanks for reporting, I’m on it.

However, Nest microservices strategies are good for prototyping. Still, I’d recommend using a mature solution like, for example, NATS and plug it into Nest as a Custom Transport Strategy (which should be very easy). It will change in the incoming release (v5.0.0) where microservices will offer much more than before.

0reactions
lock[bot]commented, Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle concurrency in microservices - Quora
In context of microservices, concurrency represents the ability to handle multiple parallel API requests or backend events operating on top of the same...
Read more >
Solving Concurrency in Event-Driven Microservices - ITNEXT
A race condition can make the service publish the same subscription twice. If the service handles two stock changed events (for example, from...
Read more >
Microservice Threading Models and their Tradeoffs - InfoQ
Because threads must share stateful database connections, developers must be able to identify and fix concurrency bugs such as deadlock, ...
Read more >
Microservices Concurrent Payments on The Same Account
The solution for financial transactions is: 1.Any transaction on account A is a put into a queue that must have account A in...
Read more >
Common Performance Problems With Microservices - JRebel
One of the most effective ways to reduce overhead in microservices that access and alter databases (aside from caching) is to pool their...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found