on 2025 Mar 22 8:56 PM
I'm investigating a potential race condition in an SAP CAP service where req.user or cds.context might be overwritten by another request during an internal POST call.
Here is a simplified version of the code:
const cds = require('@sap/cds'); const LOG = cds.log(); module.exports = class TesteRace extends cds.ApplicationService { init() { this.on('TesteC', async (req) => { try { const srv = await cds.connect.to('TesteRace'); LOG.info(`Executing TesteC - User from req.user: ${req.user?.attr?.email || req?.user?.id || 'empty'}`); LOG.info(`Executing TesteC - User from cds.context: ${cds.context.user.attr.email || cds.context?.user?.id || 'empty'}`); return await srv.post(this.entities.TesteA).entries([{ nome: req?.data?.nome || Math.random() * 1000 }]); } catch (error) { req.error(error); } }); this.before('CREATE', this.entities.TesteA, async (req) => { req.data.nome = `${req.user?.attr?.email || cds.context?.user?.id || cds.context?.user?.attr?.email} ${Math.random() * 1000}`; LOG.info(`Before CREATE TesteA - User from req.user: ${req.user.attr.email || 'empty'}`); LOG.info(`Before CREATE TesteA - User from cds.context: ${cds.context?.user?.id || cds.context?.user?.attr?.email || 'empty'}`); }); this.before('CREATE', this.entities.TesteB, async (req) => { req.data.nome = `${req.user?.attr?.email || cds.context?.user?.id || cds.context?.user?.attr?.email} ${Math.random() * 1000}`; LOG.info(`Before CREATE TesteB - User from req.user: ${req.user.attr.email || 'empty'}`); LOG.info(`Before CREATE TesteB - User from cds.context: ${cds.context?.user?.id || cds.context?.user?.attr?.email || 'empty'}`); }); return super.init(); } }
Has anyone experienced a similar issue? Could cds.context unexpectedly be shared between requests? Any insights would be greatly appreciated! 🚀
Request clarification before answering.
Hello @fabi2295 ,
Please verify if SAP CAP's locking mechanisms can help prevent req.user and cds.context from being overwritten by concurrent requests.
Please refer: https://cap.cloud.sap/docs/node.js/cds-ql#forupdate
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
81 | |
12 | |
9 | |
8 | |
8 | |
5 | |
4 | |
4 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.